PHP截取指定圖片大小的方法_php技巧

來源:互聯網
上載者:User

本文執行個體講述了PHP截取指定圖片大小的方法。分享給大家供大家參考。具體分析如下:

imagecopyresampled($newim, $im, 0, 0, 7, 174, 120, 42, $new_img_width, $new_img_height); // 原始大小 120 x 42

imagecopyresampled($newim, $im, 0, 0, 100, 30, 500, 500, $new_img_width, $new_img_height); // 圖截出來後放到 500 x 500

imagecopyresampled($newim, $im, 0, 0, 100, 30, 10, 10, $new_img_width, $new_img_height); // 圖截出來後縮小到 10 x 10

大概主要是這個 function 的操作而已,主要只看第一個原尺寸的那個參數對照就好了,下述是 imagecopyresampled 的參數,一個一個對照如下:

$newim: imagecreate($new_img_width, $new_img_height); 建立的空白圖層

$im: imagecreatefromjpeg($filename); 把原本的圖片讀進來

0: 輸出圖要從哪邊開始的 x 點(圖片輸出的點是可以指定的,沒指定到的位置就會是你 create 的圖層顏色或圖)

0: 輸出圖要從哪邊開始的 y 點(同上)

7: 原始圖要從哪邊開始 x (指定要從哪個 x 點開始截取)

174: 原始圖要從哪邊開始 y (指定要從哪個點開始截取,截取區塊大小由下面兩個參數去定)

120: 截圖要截多大 width ,從上面的指定的位置,開始截取 120px 寬(如果要放大縮小也是由此處決定)

42: 截圖要截多大 height ,從上面的指定的位置,開始截取 42px 高(如果要放大縮小也是由此處決定)

$new_img_width: 這是新的圖,從上面寫的第二組參數 0, 0 ,要開始畫多寬(在此程式目前是指定截圖的大小)

$new_img_height: 這是新的圖,從上面的第二組參數 0, 0 ,要開始畫多高(在此程式目前是指定截圖的大小)

PHP執行個體代碼如下:

複製代碼 代碼如下:
header("Content-type: image/jpeg"); 
 
$filename = 'book_rabbit_rule.jpg';
/* 讀取圖檔 */
$im = imagecreatefromjpeg($filename); 
/* 圖片要截多少, 長/寬 */
$new_img_width = 120;
$new_img_height = 42; 
/* 先建立一個 新的空白圖檔 */
$newim = imagecreate($new_img_width, $new_img_height); 
// 輸出圖要從哪邊開始 x, y ,原始圖要從哪邊開始 x, y ,要畫多大 x, y(resize) , 要抓多大 x, y
imagecopyresampled($newim, $im, 0, 0, 7, 174, 120, 42, $new_img_width, $new_img_height); 
/* 放大成 500 x 500 的圖 */
// imagecopyresampled($newim, $im, 0, 0, 100, 30, 500, 500, $new_img_width, $new_img_height); 
/* 將圖印出來 */
imagejpeg($newim); 
/* 資源回收 */
imagedestroy($newim);
imagedestroy($im);

希望本文所述對大家的PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.