PHP截取指定圖片大小執行個體代碼_PHP教程

來源:互聯網
上載者:User
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);
?>

檔案內大概該有的註解都寫好了(Source code 放在此篇最下面),解釋一下主要參數即可:

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 ,要開始畫多高(在此程式目前是指定的大小)


http://www.bkjia.com/PHPjc/444961.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444961.htmlTechArticleheader(Content-type:image/jpeg); $filename='book_rabbit_rule.jpg'; /*讀取圖檔*/ $im=imagecreatefromjpeg($filename); /*圖片要截多少,長/寬*/ $new_img_width=120; $new_img_...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.