Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
<?php function resizeFile($source, $destination, $newwidth, $newheight, $chmod=0777) { $thumb = resize($source, $newwidth, $newheight); if ($thumb) { // mkdir(dirname($destination)); // @TODO: ensure destination exists imagejpeg($thumb, $destination); chmod($destination, $chmod); // file rights } else { return false; // throwing an exception is better that this ... } } function resize($source, $newwidth, $newheight) { if (!is_file($source)) { return false; } $image = null; if (preg_match("/.jpg/i", "$source")) { // mimeType: image/jpeg $image = imagecreatefromjpeg($source); } else if (preg_match("/.gif/i", "$source")) { // mimeType: image/gif $image = imagecreatefromgif($source); } else if (preg_match("/.png/i", "$source")) { // mimeType: image/png $image = imagecreatefrompng($source); } else { return false; // unknown format } list($width, $height) = getimagesize($source); if($newwidth < $width && $newheight < $height){ $widthdiff = $width -$newwidth; $heightdiff = $height -$newheight; if($heightdiff > $widthdiff){ $newwidth = ceil( $width*$newheight/$height); }else{ $newheight = ceil( $height*$newwidth/$width); } }else if($newwidth < $width){ $newheight = ceil( $height*$newwidth/$width); }else if($newheight < $height){ $newwidth = ceil( $width*$newheight/$height); } else { $newwidth = $width; $newheight = $height; } $gd_version = gd_version(); //$gd_version = 3; $thumb = null; if ($gd_version >= 2) { $thumb = imagecreatetruecolor($newwidth, $newheight); } else { $thumb = imagecreate($newwidth, $newheight); } // imagealphablending($thumb, false); // Resize //imagecopyresized($thumb, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // Resample imagecopyresampled($thumb, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); return $thumb; } function image2jpeg($source, $destination, $type=null) { if (!is_file($source)) { return false; } $image = null; // alternatively & better: get file extension and use it instead of preg_match if (preg_match("/.jpg$/i", $source) ||preg_match("/.jpeg$/i", $source) ||"jpg"==$type) { // mimeType: image/jpeg $image = imagecreatefromjpeg($source); } else if (preg_match("/.gif$/i", $source) || "gif"==$type) { // mimeType: image/gif $image = imagecreatefromgif($source); } else if (preg_match("/.png$/i", $source) || "png"==$type) { // mimeType: image/png $image = imagecreatefrompng($source); } else { return false; // unknown format } if (!$image) { return false; } mkpath(dirname($destination)); imagejpeg($image, $destination); return true; } function webShowImage($source) { Header("Content-type: image/jpeg"); $im = imagecreatefromjpeg("$source"); Imagejpeg($im, '', 100); ImageDestroy($im); } function webResizeImage($source, $newwidth, $newheight) { $thumb = resize($source, $newwidth, $newheight); header('Content-type: image/jpeg'); imagejpeg($thumb); } function gd_version() { static $gd_version_number = null; if ($gd_version_number === null) { // Use output buffering to get results from phpinfo() // without disturbing the page we're in. Output // buffering is "stackable" so we don't even have to // worry about previous or encompassing buffering. ob_start(); phpinfo(INFO_MODULES); // or 8 $module_info = ob_get_contents(); ob_end_clean(); if (preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info,$matches)) { $gd_version_number = $matches[1]; } else { $gd_version_number = 0; } } return $gd_version_number; } ?> |