The clipping and shrinking of the PHP picture is a _php instance of the thumbnail image conforming to the requirement

Source: Internet
Author: User
The picture is too large and the specification is not uniform, the display control needs to rely on JavaScript to complete, use on the mobile device when the display is not good and the flow is huge, need to have a picture of the existing picture library to do a processing, produce the thumbnail that conforms to the mobile device, The original client JS do work transferred to the server side with PHP's GD library to centralize processing.

picture source and required size
Copy Code code as follows:

$src _img = "wallpaper.jpg";
$DST _w = 300;
$DST _h = 200;

Trims the image to ensure that the image area is maximized and scaled to the specified size.

At first, the Imagecopyresized method was used to reduce the image ratio, and after the actual operation, it was found that the dry point was very serious after the image was reduced. And then replace with imagecopyresampled (here to say, online reprint this article a lot of, but they all put imagecopyresampled written imagecopysampled cause can not use, so I just pasted this) method, The method can resample the image and smooth the reduced image, so the sharpness can be improved greatly.
Copy Code code as follows:

<?php
List ($src _w, $src _h) =getimagesize ($src _img); Get the original size
$DST _scale = $dst _h/$dst _w; Target image aspect ratio
$src _scale = $src _h/$src _w; Aspect ratio of original artwork
if ($src _scale>= $dst _scale)
{
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;
}
Cutting
$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);
?>

Hope you can use, or more convenient.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.