Home >Backend Development >PHP Tutorial >Crop and zoom PHP images to generate thumbnails that meet your needs_PHP tutorial

Crop and zoom PHP images to generate thumbnails that meet your needs_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:14:11892browse

The images are too large and have inconsistent specifications. The display control needs to be completed by JavaScript. When used on mobile devices, the display effect is not good and the traffic is huge. The images in the existing image library need to be processed once to generate an abbreviation suitable for mobile devices. As shown in the sketch, the original work done by JS on the client side is transferred to the server side for centralized processing using PHP's GD library.

Image source and required size:

Copy code The code is as follows:

$src_img = "wallpaper.jpg";
$dst_w = 300;
$dst_h = 200;

Crop the image to ensure that the image area is maximized and scaled to Specify size.

I initially used the imagecopyresized method to reduce the image proportionally. After actual operation, I found that the image was very dry after being reduced. Then switch to the imagecopyresampled (Let me tell you here, there are many reprints of this article on the Internet, but they all write imagecopyresampled as imagecopysampled, which makes it unusable, so I reposted this) method. This method will resample the image and reduce the size of the image. The image is smoothed to greatly improve the clarity.
Copy code The code is as follows:

list($src_w,$src_h)=getimagesize( $src_img); //Get original image size
$dst_scale = $dst_h/$dst_w; //Target image aspect ratio
$src_scale = $src_h/$src_w; // Original image aspect ratio
if($src_scale>=$dst_scale)
{
// Too high
$w = intval($src_w);
$h = intval($dst_scale*$w);
$x = 0;
$y = ($src_h - $h)/3;
}
else
{
// Too wide
$h = intval($ src_h);
$w = intval($h/$dst_scale);
$x = ($src_w - $w)/2;
$y = 0;
}
/ / Crop
$source=imagecreatefromjpeg($src_img);
$croped=imagecreatetruecolor($w, $h);
imagecopy($croped,$source,0,0,$x,$y, $src_w,$src_h);
// Scaling
$scale = $dst_w/$w;
$target = imagecreatetruecolor($dst_w, $dst_h);
$final_w = intval($w *$scale);
$final_h = intval($h*$scale);
imagecopyresampled($target,$croped,0,0,0,0,$final_w,$final_h,$w,$h );
// Save
$timestamp = time();
imagejpeg($target, "$timestamp.jpg");
imagedestroy($target);
?>

I hope everyone can use it, it is quite convenient.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326362.htmlTechArticleThe picture is too large and the specifications are not uniform. The display control needs to be completed by JavaScript. It is displayed when used on mobile devices. The effect is not good and the traffic is huge. It is necessary to update the pictures in the existing picture library...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn