簡單php 圖片比例縮放代碼

來源:互聯網
上載者:User

我利用了getimagesize來擷取原圖片的大小然後再x0.5就是把圖片/5哦。

array getimagesize ( string $filename [, array &$imageinfo ] )
getimagesize() 函數將測定任何 gif,jpg,png,swf,swc,ps教程d,tiff,bmp,iff,jp2,jpx,jb2,jpc,xbm 或 wbmp 影像檔的大小並返回映像的尺寸以及檔案類型和一個可以用於普通 html 檔案中 <img> 標記中的 height/width 文本字串。
如果不能訪問 filename 指定的映像或者其不是有效映像,getimagesize() 將返回 false 併產生一條 e_warning 級的錯誤

*/
//定義一個檔案
$filename='1.jpg';
$percent=0.5;
//發送頭部檔案
header('content-type: image/jpeg');
//擷取映像的大小
list($width,$height)=getimagesize($filename);
//定義新的大小
$new_width=$width * $percent;
$new_height=$height * $percent;
$image_p=imagecreatetruecolor($new_width, $new_height);
/*
: int imagecreate(int x_size, int y_size);

傳回值: 整數

內容說明


本函數用來建立一張全空的圖形。參數 x_size、y_size 為圖形的尺寸,單位為像素 (pixel)。

*/
$image=imagecreatefromjpeg($filename);

/*
resource imagecreatefromjpeg ( string filename )   imagecreatefromjpeg() 返回一映像標識符,代表了從給定的檔案名稱取得的映像。   imagecreatefromjpeg() 在失敗時返回一個Null 字元串,並且輸出一條錯誤資訊,不幸地在瀏覽器中顯示為斷連結。為減輕調試工作下面的例子會產生一個錯誤 jpeg:
*/
imagecopyresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);

/*
):imagecopyresamples() ,其像素插值演算法得到的映像邊緣比較平滑.品質較好(但該函數的速度比 imagecopyresized() 慢).   兩個函數的參數是一樣的.如下:   imagecopyresampled(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);
*/
//輸出映像
imagejpeg($image_p, null, 100);
/*
執行該代碼,將把原映像縮放50%,並以新映像輸出
*/

聯繫我們

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