2017-05-06 169 views
0

對於我正在構建的網站,我需要調整一些圖片的大小以使網頁加載更輕。我用這個代碼:圖片調整大小php

list($width, $height, $type, $attr) = getimagesize($img_path); 
    $thumb = imagecreatetruecolor(50, 50 * $height/$width); 
    if($type == 2) $source = imagecreatefromjpeg($img_path); 
    if($type == 3) $source = imagecreatefrompng($img_path); 
    imagecopyresized($thumb, $source, 0, 0, 0, 0, 50, 50 * $height/$width, $width, $height); 

現在我可以將它們保存在服務器,讓他們做好準備在必要時但考慮到小空間我可以,我寧願避免。

所以我的問題是:有沒有辦法從我已經有的代碼中創建一個「臨時」圖像並在<img>標籤內使用它?

例如<img src="$img_resized"/>

+0

看看[TimThumb](https://www.binarymoon.co.uk/projects/ timthumb /) – gaganshera

+0

參考這個http://stackoverflow.com/questions/3971841/how-to-resize-images-proportionally-keeping-the-aspect-ratio –

回答

0

這就是所謂Resizing Images on the Fly

我建議你使用這些庫之一是:

meenie/munee

mos/cimage

使用composer安裝庫:

composer require meenie/muneecomposer require mos/cimage

然後你就可以使用這些鏈接一個你的照片:

Meenie: <img src="/path/to/image.jpg?resize=width[100]"> 

cImage: <img src="/host/img.php?src=test.png&width=100"> 
+0

似乎正是我需要的,但是Codeigniter是否存在任何兼容性問題框架?非常感謝。 –

+0

不,它們是框架外部的,它們應該可以工作 –