English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Directly upload the code, the imgzip($src,$newwid,$newhei) function takes in the original image, the required width for scaling, and the length for scaling. The code is all commented, if you don't understand, you can leave a message ha ha
<?php //Compress image, thumbnail $src= "xiezheng.jpg"; $newwid=640; $newhei= 480; function imgzip($src,$newwid,$newhei){ $imgInfo = getimagesize($src); $imgType = image_type_to_extension($imgInfo[2], false); $fun = "imagecreatefrom{$imgType}"; //Declare image, open image, in memory $image = $fun($src); //Convenient to configure length, width, and height, set the frame as the variable wid, and the height as hei $wid=$imgInfo[0]; $hei=$imgInfo[1]; //Determine the length and width to facilitate proportional scaling, specifications are according to500, 320 if($wid>$hei){ $wid=$newwid; $hei=$newwid/($wid/$hei); }else { $wid = $newhei * ($wid / $hei); $hei = $newhei; } //Create a picture in memory $images2 = imagecreatetruecolor($newwid, $newhei); //Establish a500*320 image //Copy the original image to the new image //imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) imagecopyresampled($images2, $image, 0, 0, 0, 0, $wid,$hei, $imgInfo[0],$imgInfo[0]); //Destroy the original image imagedestroy($image); //Directly output the image file header("Content-type: ".$imgInfo[‘mime’]); imagejpeg($images2); //Save the image to a new file imagejpeg($images2, ‘new.jpg’ 100); //10The quality of the output image code is 0-100 100 - Highest Quality //Destroy imagedestroy($images2); } imgzip($src,$newwid,$newhei); ?>
Effect
Before Compression
Compressed
This is the compilation of materials for proportional scaling images in PHP and other languages, and more related materials will be supplemented in the future. Thank you for your support to our website!