PHP Generate thumbnail image

Source: Internet
Author: User

/**
* Generate thumbnail image
* @param string Source graph absolute full address {with file name and suffix name}
* @param string Target graph absolute full address {with file name and suffix name}
* @param int thumbnail width {0: At this point the target height cannot be 0, the target width is the source plot width * (target height/source graph height)}
* @param int thumbnail height {0: At this time the target width cannot be 0, the target height is the source graph high * (target width/source wide chart)}
* @param int is cut {width, height must not 0}
* @param int/float Zoom {0: Not scaled, 0<this<1: Zoom to the appropriate scale (at which time the width and height limits and cropping are invalidated)}
* @return Boolean
*/
public static function Img2thumb ($src _img, $dst _img, $width =, $height = 0, $cut = 0, $proportion
{
if (!is_file ($src _img))
{
return false;
}
Echo ' CCCCCC '. " \ n ";
$ot = Commonmethod::fileext ($dst _img);
$otfunc = ' image '. ($ot = = ' jpg '? ' JPEG ': $ot);
$srcinfo = getimagesize ($src _img);
$SRC _w = $srcinfo [0];
$SRC _h = $srcinfo [1];
$type = Strtolower (substr (Image_type_to_extension ($srcinfo [2]), 1));
$createfun = ' Imagecreatefrom '. ($type = = ' jpg '? ' JPEG ': $type);

$DST _h = $height;
$DST _w = $width;
$x = $y = 0;

/**
* Thumbnails do not exceed the size of the source map (provided that there is only one width or height)
*/
if ($width > $src _w && $height > $src _h) | | ($height > $src _h && $width = = 0) | | ($width > $src _w && $height = = 0))
{
$proportion = 1;
}
if ($width > $src _w)
{
$dst _w = $width = $src _w;
}
if ($height > $src _h)
{
$DST _h = $height = $src _h;
}
if (! $width &&! $height &&! $proportion)
{
return false;
}
if (! $proportion)
{
if ($cut = = 0)
{
if ($dst _w && $dst _h)
{
if ($dst _w/$src _w> $dst _h/$src _h)
{
$DST _w = $src _w * ($dst _h/$src _h);
$x = 0-($dst _w-$width)/2;
}
Else
{
$DST _h = $src _h * ($dst _w/$src _w);
$y = 0-($dst _h-$height)/2;
}
}
else if ($dst _w xor $dst _h)
{
if ($DST _w &&! $DST _h)//Width No height
{
$propor = $dst _w/$src _w;
$height = $dst _h = $src _h * $PROPOR;
}
else if (! $DST _w && $dst _h)//with high or no width
{
$propor = $dst _h/$src _h;
$width = $dst _w = $src _w * $PROPOR;
}
}
}
Else
{
if (! $DST _h)//crop without high
{
$height = $dst _h = $dst _w;
}
if (! $DST _w)//no width when cropping
{
$width = $dst _w = $DST _h;
}
$propor = min (max ($dst _w/$src _w, $dst _h/$src _h), 1);
$DST _w = (int) round ($src _w * $propor);
$DST _h = (int) round ($src _h * $propor);
$x = ($width-$dst _w)/2;
$y = ($height-$dst _h)/2;
}
}
Else
{
$proportion = min ($proportion, 1);
$height = $dst _h = $src _h * $proportion;
$width = $dst _w = $src _w * $proportion;
}

$SRC = $createfun ($src _img);
$DST = Imagecreatetruecolor ($width? $width: $dst _w, $height? $height: $dst _h);
$white = Imagecolorallocate ($DST, 255, 255, 255);
Imagefill ($dst, 0, 0, $white);
Echo ' 11111 '. \ n ";
if (function_exists (' imagecopyresampled '))
{
Imagecopyresampled ($DST, $SRC, $x, $y, 0, 0, $dst _w, $dst _h, $src _w, $src _h);
}
Else
{
Imagecopyresized ($DST, $SRC, $x, $y, 0, 0, $dst _w, $dst _h, $src _w, $src _h);
}
Echo ' 222222 '. \ n ";
$otfunc ($DST, $dst _img);
Imagedestroy ($DST);
Imagedestroy ($SRC);
return true;
}

PHP Generate thumbnail image

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.