图片调整大小php

问题描述:

对于我正在构建的网站,我需要调整一些图片的大小以使网页加载更轻。我用这个代码:图片调整大小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://*.com/questions/3971841/how-to-resize-images-proportionally-keeping-the-aspect-ratio –

这就是所谓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

不,它们是框架外部的,它们应该可以工作 –