PHP實(shí)現(xiàn)圖片裁剪及縮放函數(shù)
來(lái)源:易賢網(wǎng) 閱讀:1280 次 日期:2015-04-16 15:54:40
溫馨提示:易賢網(wǎng)小編為您整理了“PHP實(shí)現(xiàn)圖片裁剪及縮放函數(shù)”,方便廣大網(wǎng)友查閱!

PHP實(shí)現(xiàn)圖片裁剪及縮放函數(shù),根據(jù)自定義的目標(biāo)圖高寬, 按目標(biāo)圖高寬比裁剪、縮放源圖.

傳入?yún)?shù):

$source_path string 源圖路徑

$target_width integer 目標(biāo)圖寬度

$target_height integer 目標(biāo)圖高度

源圖支持MIMETYPE: image/gif, image/jpeg, image/png。具體代碼如下:

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;

// 源圖過(guò)高

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;

}

// 源圖過(guò)寬

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;

}

// 源圖適中

else

{

$cropped_width = $source_width;

$cropped_height = $source_height;

$source_x = 0;

$source_y = 0;

}

switch ($source_mime)

{

case 'image/gif':

$source_image = imagecreatefromgif($source_path);

break;

case 'image/jpeg':

$source_image = imagecreatefromjpeg($source_path);

break;

case 'image/png':

$source_image = imagecreatefrompng($source_path);

break;

default:

return false;

break;

}

$target_image = imagecreatetruecolor($target_width, $target_height);

$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

// 裁剪

imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);

// 縮放

imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);

header('Content-Type: image/jpeg');

imagejpeg($target_image);

imagedestroy($source_image);

imagedestroy($target_image);

imagedestroy($cropped_image);

}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看技術(shù)文章
易賢網(wǎng)手機(jī)網(wǎng)站地址:PHP實(shí)現(xiàn)圖片裁剪及縮放函數(shù)
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)