PHP Imagecreatetruecolor Image cropping
Any image cropped to a fixed size, the image is not deformed, the blank stretch fill, the image is always paved, do not leave blank, used bcastr friends should know, BCASTR is to ensure that the image call is not distorted, for a fixed size output image box, the source diagram has the following several situations:
1: Need to output the height of the image of the width is small, written judgement $new _width< $src _width && $new _height< $src _width
2: Need to output the image of the width is larger than the original, written judgement $new _width> $src _width && $new _height> $src _width
3: Excluding the 1th, 22, that is, one side magnified, while narrowing the situation plus equal judgment
For 1,2, the function processing code is exactly the same, so it can be summed up as a processing statement
Give the PHP implementation code
<?php
/*
* Description: Function function is to cut an image to any size of the image, the image does not distort
* Parameter Description: Enter the file name that you want to process the picture, generate the file name of the new picture, generate the new picture's width, generate the new picture high
* Written by Smallchicken
* Time 2008-12-18
*/
To obtain any size image, insufficient local stretching, no deformation, no left blank
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 high width than the original is small, or large, the principle is to take a large proportion of magnification, take a large proportion of the reduction (the proportion of smaller)
if ($ratio _w < 1 && $ratio _h < 1) | | ($ratio _w > 1 && $ratio _h > 1))
{
if ($ratio _w < $ratio _h)
{
$ratio = $ratio _h; The proportion of the width is smaller than the height direction, and is cut or enlarged according to the height of the proportional standard.
}else {
$ratio = $ratio _w;
}
Defines a temporary image in the middle that meets the target requirements for the width-height ratio of the image
$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 and 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 A side of the target image is larger than the original, one edge is smaller than the original, enlarge the image and then crop
=if (($ratio _w < 1 && $ratio _h > 1) | | ($ratio _w >1 && $ratio _h <1))
else{
$ratio = $ratio _h> $ratio _w? $ratio _h: $ratio _w; Take the value of the large proportion
Defines a large middle image in which the image is equal in height or width and the target image is then enlarged
$inter _w= (int) ($W * $ratio);
$inter _h= (int) ($H * $ratio);
$inter _img=imagecreatetruecolor ($inter _w, $inter _h);
Scale the original image and then crop
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