PHP
·
发表于 5年以前
·
阅读量:8308
±¾ÎĽeÉÜÁËPHPͼƬ²Ã¼oÓeËoÅʾÀý£¬Ï»°²»¶aÉÙ£¬¾ßÌa´uÂeÈçÏ£º
/*
*exif_imagetype -- ÅжÏÒ»¸oͼÏñµÄÀaÐÍ
*¹¦ÄÜ˵Ã÷£ºº¯Êý¹¦ÄÜÊÇ°ÑÒ»¸oͼÏñ²Ã¼oΪÈÎÒa´oСµÄͼÏñ£¬²¢±£³ÖͼÏñ²»±aÐÎ
*²ÎÊý˵Ã÷£ºÊaÈe ÐeÒª´¦ÀiͼƬµÄ ÎļþÃu£¬Éu³ÉÐÂͼƬµÄ±£´aeÎļþÃu£¬Éu³ÉÐÂͼƬµÄ¿i£¬Éu³ÉÐÂͼƬµÄ¸ß
*/
// »ñµÃÈÎÒa´oСͼÏñ£¬²»×aµØ*½ÀÉi£¬²»²uÉu±aÐΣ¬²»ÁoÏ¿հ×
function image_resize($src_file, $dst_file, $new_width, $new_height)
{
$new_width = intval($new_width);
$new_height = intval($new_width);
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();
}
// ͼÏñÀaÐÍ
$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;
// Éu³ÉµÄͼÏñµÄ¸ß¿i±ÈÔÀ´µÄ¶¼Ð¡£¬»o¶¼´o £¬ÔÔoÊÇ È¡´o±ÈÀý*Å´o£¬È¡´o±ÈÀýËoС£¨ËoСµÄ±ÈÀý¾Í±È½ÏСÁË£(C)
if (($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1))
{
if ($ratio_w < $ratio_h) {
$ratio = $ratio_h;
// Çe¿oÒ»£¬¿i¶ÈµÄ±ÈÀý±È¸ß¶È*½ÏoµÄС£¬°´Õո߶ȵıÈÀý±e×¼À´²Ã¼o»o*Å´o
} else {
$ratio = $ratio_w;
}
// ¶¨ÒaÒ»¸oÖмaµÄÁÙʱͼÏñ£¬¸ÃͼÏñµÄ¿i¸ß±È ÕýºÃÂu×aÄ¿±eÒªÇo
$inter_w = (int)($new_width / $ratio);
$inter_h = (int)($new_height / $ratio);
$inter_img = imagecreatetruecolor($inter_w, $inter_h);
//var_dump($inter_img);
imagecopy($inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h);
// Éu³ÉÒ»¸oÒÔ×i´o±ß³¤¶ÈΪ´oСµÄÊÇÄ¿±eͼÏñ$ratio±ÈÀýµÄÁÙʱͼÏñ
// ¶¨ÒaÒ»¸oеÄͼÏñ
$new_img = imagecreatetruecolor($new_width, $new_height);
//var_dump($new_img);exit();
imagecopyresampled($new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height, $inter_w, $inter_h);
switch($type)
{
case IMAGETYPE_JPEG :
// ´ae´¢Í¼Ïñ
imagejpeg($new_img, $dst_file, 100);
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 Ä¿±eͼÏñ µÄÒ»¸o±ß´oÓÚÔͼ£¬Ò»¸o±ßСÓÚÔͼ £¬ÏÈ*Å´oƽÆÕͼÏñ£¬È»ºo²Ã¼o
// =if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w >1 && $ratio_h <1) )
else {
$ratio = $ratio_h > $ratio_w ? $ratio_h : $ratio_w;
//È¡±ÈÀý´oµÄÄǸoÖµ
// ¶¨ÒaÒ»¸oÖмaµÄ´oͼÏñ£¬¸ÃͼÏñµÄ¸ß»o¿iºÍÄ¿±eͼÏñÏaµÈ£¬È»ºo¶ÔÔͼ*Å´o
$inter_w = (int)($w * $ratio);
$inter_h = (int)($h * $ratio);
$inter_img = imagecreatetruecolor($inter_w, $inter_h);
//½«ÔͼËo*űÈÀýºo²Ã¼o
imagecopyresampled($inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h, $w, $h);
// ¶¨ÒaÒ»¸oеÄͼÏñ
$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 :
// ´ae´¢Í¼Ïñ
imagejpeg($new_img, $dst_file, 100);
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
//Êa³oÐÂͼƬ
image_resize('test.jpg', 'demo.jpg', '1200px', '1200px');
ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£