I used some PHP implementation of the image size adjustment (image resize) function, but are not very perfect, some pictures in the adjustment after the complete deformation, there is no reason for some more lines, and some in the color brightness is also passable. Today, I looked again, and there was some harvest.
if (Isset ($_files[' Manager_photo ')) &&!empty ($_files[' Manager_photo ' [' name '])) {$photo _name= $manager _n Ame. " -". $_files[" Manager_photo "[" Name "]; $tmpname = $_files[' Manager_photo ' [' tmp_name ']; $file _ext= substr ($photo _name, Strripos ($photo _name, '. ')); $dir = "./manager_photos/"; Finally resize @img_resize ($tmpname, $dir, $photo _name, 1);} function Img_resize ($tmpname, $size, $save _dir, $save _name, $maxisheight = 0) {$save _dir. = (substr ($save _di R,-1)! = "/")? "/" : ""; $gis = getimagesize ($tmpname); $type = $gis [2]; Switch ($type) {case "1": $imorig = Imagecreatefromgif ($tmpname); Case "2": $imorig = Imagecreatefromjpeg ($tmpname); Case "3": $imorig = Imagecreatefrompng ($tmpname); Break Default: $imorig = Imagecreatefromjpeg ($tmpname); } $x = Imagesx ($imorig); $y = Imagesy ($imorig); $woh = (! $maxisheight)? $gis [0]: $gis [1]; if ($woh <= $size) {$aw = $x; $ah = $y; } else {if (! $maxisheight) {$aw = $size; $ah = $size * $y/$x; } else {$aw = $size * $x/$y; $ah = $size; }} $im = Imagecreatetruecolor ($aw, $ah); if (imagecopyresampled ($im, $imorig, 0,0,0,0, $aw, $ah, $x, $y)) if (imagejpeg ($im, $save _dir. $save _name)) r Eturn true; else return false;} Img_resize
Using ImageMagick to achieve
Someone mentioned using ImageMagick to achieve, it has a lot of options, we can use very little code to achieve picture adjustment. Worthy of study.
$image = new Imagick ($SRC); $image->resizeimage ($width, $height, imagick::filter_lanczos,1); $image->cropimage ($width _2, $height _2, $x, $y); $image->setcompression (imagick::compression_jpeg); $image->setimagecompressionquality (82); $image->writeimage ($dest); $image->destroy ();
Implemented with Php_class_upload
If the introduction is not suitable for you, you can not only picture upload and picture adjustment, but also can cut, cut, add watermark, rotate, flip and so on.
Other implementation code references
This code can return a picture with the correct name, but only black?
function Resize ($originalImage) {list ($width, $height) = getimagesize ($originalImage); $newName =basename ($ Originalimage); $imageResized = Imagecreatetruecolor (;), $imageTmp = Imagecreatefromjpeg ($originalImage); Imagecopyresampled ($imageResized, $imageTmp, 0, 0, 0, 0, N, a, $width, $height); Imagejpeg ($imageResized, "resizedimg /$newName ", Imagedestroy); ($imageResized);}
After the code improves, look at the following code to set the color.
function Resizeimage ($file) {define (' Max_width '),//max image WIDTH define (' Max_height ' ,//max image Height define (' max_file_size ', 10485760); Iamge save Path $path = ' storeresize/'; Size of the resize image $new _width = 128; $new _height = 128; Name of the new image $nameOfFile = ' resize_ '. $new _width. ' x '. $new _height. ' _ '. basename ($file [' name ']); $image _type = $file [' type ']; $image _size = $file [' Size ']; $image _error = $file [' ERROR ']; $image _file = $file [' Tmp_name ']; $image _name = $file [' name ']; $image _info = getimagesize ($image _file); Check image type if ($image _info[' mime '] = = ' Image/jpeg ' or $image _info[' mime '] = = ' image/jpg ') {} else if ($image _info[' mime '] = = ' Image/png ') {} else if ($image _info[' mime '] = = ' Image/gif ') { } else{ Set error Invalid file type} if ($image _error) {//set error image upload error} if ($image _size > max_file_size) {//set error image size invalid} switch ($image _info[' mi Me ']) {case ' image/jpg '://this isn ' t a valid MIME type so we should probably remove it case ' image /jpeg ': $image = imagecreatefromjpeg ($image _file); Break Case ' image/png ': $image = imagecreatefrompng ($image _file); Break Case ' image/gif ': $image = imagecreatefromgif ($image _file); Break } if ($new _width = = 0 && $new _height = = 0) {$new _width = 100; $new _height = 100; }//ensure size limits can not be abused $new _width = min ($new _width, max_width); $new _height = min ($new _height, max_height); Get original image h/w $width = Imagesx ($image); $height = Imagesy ($image); $align = ' B '; $zoom _crop = 1; $origin _x = 0; $origin _y = 0; TODO setting Memory//Generate new w/h if not provided if ($new _width &&! $new _height) { $new _height = Floor ($height * ($new _width/$width)); } else if ($new _height &&! $new _width) {$new _width = Floor ($width * ($new _height/$height)); }//Scale down and add borders if ($zoom _crop = = 3) {$final _height = $height * ($new _width/$wid TH); if ($final _height > $new _height) {$new _width = $width * ($new _height/$height); } else {$new _height = $final _height; }}//Create a new true color image $canvas = Imagecreatetruecolor ($new _width, $new _height); Imagealphablending ($canvas, false); if (strlen ($canvas _color) < 6) {$canvas _color = ' ffffff '; }$canvas _color_r = Hexdec (substr ($canvas _color, 0, 2)); $canvas _color_g = Hexdec (substr ($canvas _color, 2, 2)); $canvas _color_b = Hexdec (substr ($canvas _color, 2, 2)); Create a new transparent color for image $color = Imagecolorallocatealpha ($canvas, $canvas _color_r, $canvas _col Or_g, $canvas _color_b, 127); Completely fill the background of the new image with allocated color. Imagefill ($canvas, 0, 0, $color); Scale down and add borders if ($zoom _crop = = 2) {$final _height = $height * ($new _width/$width); if ($final _height > $new _height) {$origin _x = $new _width/2; $new _width = $width * ($new _height/$height); $origin _x = round ($origin _x-($new _width/2)); } else {$origin _y = $new _HEIGHT/2; $new _height = $final _height; $origin _y = round ($origin _y-($new _height/2)); }}//Restore TranSparency blending Imagesavealpha ($canvas, true); if ($zoom _crop > 0) {$src _x = $src _y = 0; $src _w = $width; $SRC _h = $height; $CMP _x = $width/$new _width; $cmp _y = $height/$new _height; calculate x or Y coordinate and width or height of source if ($cmp _x > $cmp _y) {$src _w = round ($ width/$cmp _x * $cmp _y); $src _x = Round (($width-($width/$cmp _x * $cmp _y)/2); } else if ($cmp _y > $cmp _x) {$src _h = round ($height/$cmp _y * $cmp _x); $src _y = Round (($height-($height/$cmp _y * $cmp _x)/2); }//Positional cropping! if ($align) {if (Strpos ($align, ' t ')!== false) {$src _y = 0; } if (Strpos ($align, ' B ')!== false) {$SRC _y = $height-$src _h; } if (Strpos ($align, ' l ') {$src _x = 0;!==) { } if (Strpos ($align, ' R ')!== false) {$SRC _x = $width-$src _w; }}//Positional cropping! Imagecopyresampled ($canvas, $image, $origin _x, $origin _y, $src _x, $src _y, $new _width, $new _height, $src _w, $src _h); } else {imagecopyresampled ($canvas, $image, 0, 0, 0, 0, $new _width, $new _height, $width, $height); }//straight from Wordpress core code. Reduces filesize by-70% for PNG s if (imagetype_png = = $image _info[2] | | Imagetype_gif = = $image _info[2]) && function_exists (' Imageistruecolor ') &&!imageistruecolor ($image) && imagecolortransparent ($image) > 0) {imagetruecolortopalette ($canvas, False, Imagecolorstotal ( $image)); } $quality = 100; $nameOfFile = ' resize_ '. $new _width. ' x '. $new _height. ' _ '. basename ($file [' name ']); if (Preg_match ('/^image\/(?: jpg|jpeg) $/i ', $image _info[' mime ')) {imagejpeg ($canvas, $path. $nameOfFile, $quality); } else if (Preg_match ('/^image\/png$/i ', $image _info[' mime ')) {imagepng ($canvas, $path . $nameOfFile, Floor ($quality * 0.09)); } else if (Preg_match ('/^image\/gif$/i ', $image _info[' mime ')) {imagegif ($canvas, $path. $nameOfFile); }}