PHP cropping images into fixed size code method _php Tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg
Make a home page call image, sometimes need to get fixed-size image, because the image position of the home page is usually specified by the designer, if it is to do the latest release image call, because do not know what proportion of images customers will upload, so, sometimes there is no way to determine the proportion of the image, Front page writers usually use a fixed img high-width method to achieve control of the image does not overflow, but if the proportion of the image is not required, it will cause the image to be called after deformation, to a large extent affecting the appearance of the page, there is a solution is to scale according to the original scale, scaled image will inevitably have a blank, Blank space to fill in color, so although the image is not deformed, but this will have a lot of problems, for example, if the user sends an image is very high but the width of the general image, if compressed into a 1:1 image, then the compression after the basic will not see the image.
My solution here is that arbitrary images are clipped to a fixed size, the image is not deformed, the gap stretched fill, the image is always full, do not leave blank, used bcastr friends should know, BCASTR is to ensure that the image is not deformed, the output of a fixed size image frame, the source map has the following situations:
1: Need to output the image of the high-aspect ratio of the source graph aspect are small, written to judge $new _width< $src _width && $new _height< $src _width
2: Need to output the image of the aspect ratio of the original aspect are large, written to judge $new _width> $src _width && $new _height> $src _width
3: Exclude the 1th, 22, that is, one side of the enlargement, while narrowing the situation plus equal to the judgment
For all, the function processing code is exactly the same, so it can be summed up as a processing statement

Give the PHP implementation code

/*
* Description: Function function is to cut an image into an image of any size, the image is not deformed
* Parameter Description: Enter the file name that needs to process the picture, generate a new picture of the saved file name, create a new image width, create a new picture of the high
* Written by Smallchicken
* Time 2008-12-18
*/
Get images of any size, stretch without distortion, leave no space
function My_image_resize ($src _file, $dst _file, $new _width, $new _height) {
if ($new _width <1 | | $new _height <1) {
echo "Params width or height error!";
Exit ();
}
if (!file_exists ($src _file)) {
Echo $src _file. "is NOT exists!";
Exit ();
}
Image type
$type =exif_imagetype ($src _file);
$support _type=array (Imagetype_jpeg, Imagetype_png, imagetype_gif);
if (!in_array ($type, $support _type,true)) {
echo "This type of image does not support! Only support JPG, GIF or PNG ";
Exit ();
}
Load image
Switch ($type) {
Case IMAGETYPE_JPEG:
$src _img=imagecreatefromjpeg ($src _file);
Break
Case Imagetype_png:
$src _img=imagecreatefrompng ($src _file);
Break
Case Imagetype_gif:
$src _img=imagecreatefromgif ($src _file);
Break
Default
echo "Load image error!";
Exit ();
}
$w =imagesx ($src _img);
$h =imagesy ($src _img);
$ratio _w=1.0 * $new _width/$w;
$ratio _h=1.0 * $new _height/$h;
$ratio = 1.0;
The resulting image of the aspect ratio of the original is small, or all large, the principle is to take a large proportion of magnification, take a large proportion of the reduction (smaller proportion)
if (($ratio _w < 1 && $ratio _h < 1) | | ($ratio _w > 1 && $ratio _h > 1)) {
if ($ratio _w < $ratio _h) {
$ratio = $ratio _h; Case one, the width of the ratio is smaller than the height direction, according to the height of the proportional standard to cut or enlarge
}else {
$ratio = $ratio _w;
}
Defines an intermediate temporary image that meets the target's aspect ratio exactly.
$inter _w= (int) ($new _width/$ratio);
$inter _h= (int) ($new _height/$ratio);
$inter _img=imagecreatetruecolor ($inter _w, $inter _h);
Imagecopy ($inter _img, $src _img, 0,0,0,0, $inter _w, $inter _h);
Generates a temporary image that is the size of the maximum edge length, which is the $ratio scale of the target image
To define a new image
$new _img=imagecreatetruecolor ($new _width, $new _height);
Imagecopyresampled ($new _img, $inter _img,0,0,0,0, $new _width, $new _height, $inter _w, $inter _h);
Switch ($type) {
Case IMAGETYPE_JPEG:
Imagejpeg ($new _img, $dst _file,100); Storing images
Break
Case Imagetype_png:
Imagepng ($new _img, $dst _file,100);
Break
Case Imagetype_gif:
Imagegif ($new _img, $dst _file,100);
Break
Default
Break
}
}//End If 1
2 One edge of the target image is larger than the original, one edge is smaller than the original, the flat image is enlarged, then cropped
=if (($ratio _w < 1 && $ratio _h > 1) | | ($ratio _w >1 && $ratio _h <1))
else{
$ratio = $ratio _h> $ratio _w? $ratio _h: $ratio _w; Take a large proportion of that value
Defines an intermediate large image that is equal in height or width of the target image, and then zooms in on the original image
$inter _w= (int) ($W * $ratio);
$inter _h= (int) ($H * $ratio);
$inter _img=imagecreatetruecolor ($inter _w, $inter _h);
Crop after scaling the original
Imagecopyresampled ($inter _img, $src _img,0,0,0,0, $inter _w, $inter _h, $w, $h);
To define a new image
$new _img=imagecreatetruecolor ($new _width, $new _height);
Imagecopy ($new _img, $inter _img, 0,0,0,0, $new _width, $new _height);
Switch ($type) {
Case IMAGETYPE_JPEG:
Imagejpeg ($new _img, $dst _file,100); Storing images
Break
Case Imagetype_png:
Imagepng ($new _img, $dst _file,100);
Break
Case Imagetype_gif:
Imagegif ($new _img, $dst _file,100);
Break
Default
Break
}
}//IF3
}//End Function
?>

http://www.bkjia.com/PHPjc/486479.html www.bkjia.com true http://www.bkjia.com/PHPjc/486479.html techarticle Make a home page call image, sometimes need to get fixed-size image, because the image location of the home page is usually specified by the designer, if it is to do the latest release image call, ...

  • Related Article

    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.