1<?PHP2 3 /**4 * Image Clipping5 * @param $source _path Original Path6 * @param $target _width need to be cropped wide7 * @param $target _height need to be cropped high8 * @return BOOL9 */Ten functionImagecropper ($source _path,$target _width,$target _height) One { A $source _info=getimagesize($source _path); - $source _width=$source _info[0]; - $source _height=$source _info[1]; the $source _mime=$source _info[' MIME ']; - $source _ratio=$source _height/$source _width; - $target _ratio=$target _height/$target _width; - + //source graph too high - if($source _ratio>$target _ratio) { + $cropped _width=$source _width; A $cropped _height=$source _width*$target _ratio; at $source _x= 0; - $source _y= ($source _height-$cropped _height)/2; -}//source map too wide - ElseIf($source _ratio<$target _ratio) { - $cropped _width=$source _height/$target _ratio; - $cropped _height=$source _height; in $source _x= ($source _width-$cropped _width)/2; - $source _y= 0; to}//Moderate Source Graph + Else { - $cropped _width=$source _width; the $cropped _height=$source _height; * $source _x= 0; $ $source _y= 0;Panax Notoginseng } - $funcList=Array( the' Image/gif ' =Array( +' Create ' = ' imagecreatefromgif ', A' Make ' = ' imagegif ', the' MIME ' = '. gif ', +), -' Image/jpeg ' =Array( $' Create ' = ' imagecreatefromjpeg ', $' Make ' = ' imagejpeg ', -' MIME ' = '. jpg ', -), the' Image/png ' =Array( -' Create ' = ' imagecreatefrompng ',Wuyi' Make ' = ' imagepng ', the' MIME ' = '. png ', -), Wu ); - if(isset($funcList[$source _mime])) { About $curFunc=$funcList[$source _mime]; $}Else { - return FALSE; - } - $source _image=$curFunc[' Create '] ($source _path); A + $target _image= Imagecreatetruecolor ($target _width,$target _height); the $cropped _image= Imagecreatetruecolor ($cropped _width,$cropped _height); - $ //cropping theImagecopy ($cropped _image,$source _image, 0, 0,$source _x,$source _y,$cropped _width,$cropped _height); the //Zoom theImagecopyresampled ($target _image,$cropped _image, 0, 0, 0, 0,$target _width,$target _height,$cropped _width,$cropped _height); the - //Save the picture locally (choose one) in $randNumber=Mt_rand(00000, 99999).Mt_rand(000, 999); the //$fileName = substr (MD5 ($randNumber), 8, 16). ". png"; the $fileName= Time() . ‘_‘ .substr(MD5($randNumber), 8, 16).$curFunc[' MIME ']; About //imagepng ($target _image, './'. $fileName); the //imagepng ($target _image, './'. $fileName); the //$imgFunc 2 ($target _image, './'. $fileName); the $curFunc[' Make '] ($target _image, ‘./‘ .$fileName); +Imagedestroy ($target _image); - the //output pictures directly in the browser (choose one)Bayi /*header (' content-type:image/jpeg '); the imagepng ($target _image); the Imagedestroy ($target _image); - imagejpeg ($target _image); - Imagedestroy ($source _image); the Imagedestroy ($target _image); the Imagedestroy ($cropped _image);*/ the } the - //called the //imagecropper ('./img033.jpg ', 300,300); theImagecropper ('./4.jpg ', 140, 140); the //imagecropper ('./img033.jpg ', 55,55);94?>
<?php
/**
* Image Clipping
* @param $source _path Original path
* @param $target _width need to be cropped wide
* @param $target _height need to be cropped high
* @return BOOL
*/
function Imagecropper ($source _path, $target _width, $target _height)
{
$source _info = getimagesize ($source _path);
$source _width = $source _info[0];
$source _height = $source _info[1];
$source _mime = $source _info[' mime ');
$source _ratio = $source _height/$source _width;
$target _ratio = $target _height/$target _width;
Source Graph too high
if ($source _ratio > $target _ratio) {
$cropped _width = $source _width;
$cropped _height = $source _width * $target _ratio;
$source _x = 0;
$source _y = ($source _height-$cropped _height)/2;
}//source map too wide
ElseIf ($source _ratio < $target _ratio) {
$cropped _width = $source _height/$target _ratio;
$cropped _height = $source _height;
$source _x = ($source _width-$cropped _width)/2;
$source _y = 0;
}//Source graph Moderate
else {
$cropped _width = $source _width;
$cropped _height = $source _height;
$source _x = 0;
$source _y = 0;
}
$funcList = Array (
' Image/gif ' = Array (
' Create ' = ' imagecreatefromgif ',
' Make ' = ' imagegif ',
' MIME ' = '. gif ',
),
' Image/jpeg ' = Array (
' Create ' = ' imagecreatefromjpeg ',
' Make ' = ' imagejpeg ',
' MIME ' = '. jpg ',
),
' Image/png ' = Array (
' Create ' = ' imagecreatefrompng ',
' Make ' = ' imagepng ',
' MIME ' = '. png ',
),
);
if (Isset ($funcList [$source _mime])) {
$curFunc = $funcList [$source _mime];
} else {
return FALSE;
}
$source _image = $curFunc [' Create '] ($source _path);
$target _image = Imagecreatetruecolor ($target _width, $target _height);
$cropped _image = Imagecreatetruecolor ($cropped _width, $cropped _height);
Cutting
Imagecopy ($cropped _image, $source _image, 0, 0, $source _x, $source _y, $cropped _width, $cropped _height);
Scaling
Imagecopyresampled ($target _image, $cropped _image, 0, 0, 0, 0, $target _width, $target _height, $cropped _width, $cropped _ height);
Save the picture locally (choose one)
$randNumber = Mt_rand (00000, 99999). Mt_rand (000, 999);
$fileName = substr (MD5 ($randNumber), 8, 16). ". png";
$fileName = Time (). ‘_‘ . SUBSTR (MD5 ($randNumber), 8, 16). $curFunc [' MIME '];
Imagepng ($target _image, './'. $fileName);
Imagepng ($target _image, './'. $fileName);
$imgFunc 2 ($target _image, './'. $fileName);
$curFunc [' Make '] ($target _image, './'. $fileName);
Imagedestroy ($target _image);
Output pictures directly in the browser (choose one)
/*header (' content-type:image/jpeg ');
Imagepng ($target _image);
Imagedestroy ($target _image);
Imagejpeg ($target _image);
Imagedestroy ($source _image);
Imagedestroy ($target _image);
Imagedestroy ($cropped _image); * *
}
Call
Imagecropper ('./img033.jpg ', 300,300);
Imagecropper ('./4.jpg ', 140, 140);
Imagecropper ('./img033.jpg ', 55,55);
?>
PHP Picture Clipping