如何實(shí)現(xiàn)PHP圖片裁剪與縮放
導(dǎo)語(yǔ):如何實(shí)現(xiàn)PHP圖片裁剪與縮放,大家可以參考以下實(shí)現(xiàn)代碼,更多詳情請(qǐng)關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。
/*
*exif_imagetype -- 判斷一個(gè)圖像的類型
*功能說(shuō)明:函數(shù)功能是把一個(gè)圖像裁剪為任意大小的圖像,并保持圖像不變形
*參數(shù)說(shuō)明:輸入 需要處理圖片的 文件名,生成新圖片的保存文件名,生成新圖片的寬,生成新圖片的高
*/
/pic/p>
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();
}
/pic/p>
$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();
}
/pic/p>
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;
/pic/p>
if (($ratio_w < 1 && $ratio_h < 1) || ($ratio_w > 1 && $ratio_h > 1))
{
if ($ratio_w < $ratio_h) {
$ratio = $ratio_h;
/pic/p>
} else {
$ratio = $ratio_w;
}
/pic/p>
$inter_w = (int)($new_width / $ratio);
$inter_h = (int)($new_height / $ratio);
$inter_img = imagecreatetruecolor($inter_w, $inter_h);
/pic/p>
imagecopy($inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h);
/pic/p>
/pic/p>
$new_img = imagecreatetruecolor($new_width, $new_height);
/pic/p>
imagecopyresampled($new_img, $inter_img, 0, 0, 0, 0, $new_width, $new_height, $inter_w, $inter_h);
switch($type)
{
case IMAGETYPE_JPEG :
/pic/p>
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;
}
}/pic/p>
/pic/p>
/pic/p>
else {
$ratio = $ratio_h > $ratio_w ? $ratio_h : $ratio_w;
/pic/p>
/pic/p>
$inter_w = (int)($w * $ratio);
$inter_h = (int)($h * $ratio);
$inter_img = imagecreatetruecolor($inter_w, $inter_h);
/pic/p>
imagecopyresampled($inter_img, $src_img, 0, 0, 0, 0, $inter_w, $inter_h, $w, $h);
/pic/p>
$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 :
/pic/p>
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;
}
}/pic/p>
}/pic/p>
/pic/p>
image_resize('test.jpg', 'demo.jpg', '1200px', '1200px');
【如何實(shí)現(xiàn)PHP圖片裁剪與縮放】相關(guān)文章:
如何裁剪outlook郵件中的圖片08-03
PHP中多態(tài)如何實(shí)現(xiàn)12-26
PHP中如何實(shí)現(xiàn)crontab代碼07-15
php如何實(shí)現(xiàn)驗(yàn)證碼03-15