×öÒ»¸öÊ×Ò³µ÷ÓÃͼÏñ£¬ÓÐʱºòÍùÍùÐèÒª»ñµÃ¹Ì¶¨´óСµÄͼÏñ£¬ÒòΪÊ×Ò³µÄͼÏñλÖÃͨ³£ÓÉÉè¼ÆÈËÔ±Ö¸¶¨ºÃÁË£¬Èç¹ûÊÇ×ö×îз¢²¼Í¼Ïñµ÷Óã¬ÒòΪ²»ÖªµÀ¿Í»§»áÉÏ´«Ê²Ã´±ÈÀýµÄͼÏñ£¬ËùÒÔ£¬ÓÐʱºòÒ²¾ÍûÓа취ȷ¶¨Í¼ÏñµÄ±ÈÀý£¬Ç°Ì¨Ò³Ãæ±àдÈËԱͨ³£»á²ÉÓù̶¨ img ÔªËظ߿íµÄ°ì·¨À´´ïµ½¿ØÖÆͼÏñ²»Òç³ö£¬µ«Èç¹ûͼÏñµÄ±ÈÀý²»ÊÇÐèÒªµÄ±ÈÀý£¬¾Í»áÔì³ÉͼÏñµ÷Óúó±äÐΣ¬ºÜ´ó³Ì¶ÈÉÏÓ°ÏìÁËÒ³ÃæµÄÃÀ¹Û£¬Óнâ¾öµÄ·½·¨ÊÇ£¬°´ÕÕÔͼ±ÈÀý½øÐÐËõ·Å£¬Ëõ·ÅºóµÄͼÏñÄÑÃâ»áÓпհף¬¿Õ°×´¦ÌîÒÔÑÕÉ«£¬ÕâÑùËäȻͼÏñ²»±äÐÎÁË£¬µ«ÕâÑù»áÓкܶàÎÊÌ⣬±ÈÈ磬Èç¹ûÓû§·¢Ò»¸öͼÏñºÜ¸ßµ«¿í¶ÈÒ»°ãµÄͼÏñ£¬Èç¹ûѹËõ³ÉÒ»¸ö1£º1µÄͼÏñ£¬ÄÇôѹËõºó»ù±¾¾Í»á¿´²»µ½Í¼ÏñÁË¡£
ÎÒÕâÀïµÄ½â¾ö°ì·¨ÊÇ£¬ÈÎÒâͼÏñ²Ã¼ô³É¹Ì¶¨´óС£¬Í¼Ïñ²»±äÐΣ¬¿Õ°×´¦ÀÉìÌî³ä£¬Í¼ÏñʼÖÕÆÌÂú£¬²»Áô¿Õ°×£¬ÓùýbcastrµÄÅóÓÑÓ¦¸ÃÖªµÀ£¬bcastr¾ÍÊDZ£Ö¤ÁËͼÏñµ÷ÓõIJ»±äÐΣ¬¶ÔÒ»¸ö¹Ì¶¨´óСÊä³öµÄͼÏñ¿ò£¬Ô´Í¼ÓÐÒÔϼ¸ÖÖÇé¿ö£º
1£ºÐèÒªÊä³öµÄͼÏñµÄ¸ß¿í±ÈԴͼµÄ¸ß¿í¶¼Ð¡£¬Ð´³ÉÅÐ¶Ï $new_width<$src_width && $new_height<$src_width
2£ºÐèÒªÊä³öµÄͼÏñµÄ¸ß¿í±ÈÔͼµÄ¸ß¿í¶¼´ó£¬Ð´³ÉÅÐ¶Ï $new_width>$src_width && $new_height>$src_width
3£ºÅųýµÚ1£¬2Á½ÖÖ£¬¼´Ò»±ß·Å´ó£¬Ò»±ßËõСµÄÇé¿ö¼ÓÉϵÈÓÚµÄÅжÏ
¶ÔÓÚ1£¬2£¬º¯Êý´¦Àí´úÂëÍêÈ«Ïàͬ£¬ËùÒÔ¿ÉÒÔ¹éÄɳÉÒ»¸ö´¦ÀíÓï¾ä
¸ø³öphpʵÏÖ´úÂë
<?php
/*
*˵Ã÷£ºº¯Êý¹¦ÄÜÊÇ°ÑÒ»¸öͼÏñ²Ã¼ôΪÈÎÒâ´óСµÄͼÏñ£¬Í¼Ïñ²»±äÐÎ
* ²ÎÊý˵Ã÷£ºÊäÈë ÐèÒª´¦ÀíͼƬµÄ ÎļþÃû£¬Éú³ÉÐÂͼƬµÄ±£´æÎļþÃû£¬Éú³ÉÐÂͼƬµÄ¿í£¬Éú³ÉÐÂͼƬµÄ¸ß
* written by smallchicken
* time 2008-12-18
*/
// »ñµÃÈÎÒâ´óСͼÏñ£¬²»×ãµØ·½ÀÉ죬²»²úÉú±äÐΣ¬²»ÁôÏ¿հ×
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();
}
// ͼÏñÀàÐÍ
$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;
// Éú³ÉµÄͼÏñµÄ¸ß¿í±ÈÔÀ´µÄ¶¼Ð¡£¬»ò¶¼´ó £¬ÔÔòÊÇ È¡´ó±ÈÀý·Å´ó£¬È¡´ó±ÈÀýËõС£¨ËõСµÄ±ÈÀý¾Í±È½ÏСÁË£©
if( ($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1)) {
if($ratio_w < $ratio_h) {
$ratio = $ratio_h ; // Çé¿öÒ»£¬¿í¶ÈµÄ±ÈÀý±È¸ß¶È·½ÏòµÄС£¬°´Õո߶ȵıÈÀý±ê×¼À´²Ã¼ô»ò·Å´ó
}else {
$ratio = $ratio_w ;
}
// ¶¨ÒåÒ»¸öÖмäµÄÁÙʱͼÏñ£¬¸ÃͼÏñµÄ¿í¸ß±È ÕýºÃÂú×ãÄ¿±êÒªÇó
$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);
// Éú³ÉÒ»¸öÒÔ×î´ó±ß³¤¶ÈΪ´óСµÄÊÇÄ¿±êͼÏñ$ratio±ÈÀýµÄÁÙʱͼÏñ
// ¶¨ÒåÒ»¸öеÄͼÏñ
$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); // ´æ´¢Í¼Ïñ
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 Ä¿±êͼÏñ µÄÒ»¸ö±ß´óÓÚÔͼ£¬Ò»¸ö±ßСÓÚÔͼ £¬ÏÈ·Å´óƽÆÕͼÏñ£¬È»ºó²Ã¼ô
// =if( ($ratio_w < 1 && $ratio_h > 1) || ($ratio_w >1 && $ratio_h <1) )
else{
$ratio=$ratio_h>$ratio_w? $ratio_h : $ratio_w; //È¡±ÈÀý´óµÄÄǸöÖµ
// ¶¨ÒåÒ»¸öÖмäµÄ´óͼÏñ£¬¸ÃͼÏñµÄ¸ß»ò¿íºÍÄ¿±êͼÏñÏàµÈ£¬È»ºó¶ÔÔͼ·Å´ó
$inter_w=(int)($w * $ratio);
$inter_h=(int) ($h * $ratio);
$inter_img=imagecreatetruecolor($inter_w , $inter_h);
//½«ÔͼËõ·Å±ÈÀýºó²Ã¼ô
imagecopyresampled($inter_img,$src_img,0,0,0,0,$inter_w,$inter_h,$w,$h);
// ¶¨ÒåÒ»¸öеÄͼÏñ
$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); // ´æ´¢Í¼Ïñ
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
?>