如何将HTML块中的所有图像调整为特定宽度并缓存结果

时间:2022-11-19 13:53:05

I'm writing a mobile content adaptation plugin for a bespoke CMS in PHP. The CMS contains <img/> links to images with absolute URLs which are all 400 pixels wide and vary in height.

我正在为PHP中的定制CMS编写移动内容适配插件。 CMS包含如何将HTML块中的所有图像调整为特定宽度并缓存结果链接到图像的绝对URL,这些URL的宽度均为400像素,高度不同。

I'd like to parse the HTML (which is stored in MySQL) and re-scale each image to a new width - this will vary according to the device. I'd also like to cache the images to prevent needlessly resizing them on-the-fly every time the page is loaded

我想解析HTML(存储在MySQL中)并将每个图像重新缩放到新的宽度 - 这将根据设备而有所不同。我还想缓存图像,以防止每次加载页面时不必要地重新调整它们的大小

What's the best way for me to achieve this in PHP using either ImageMagick or GD?

使用ImageMagick或GD在PHP中实现此目的的最佳方法是什么?

2 个解决方案

#1


2  

what about doing something a bit different. basically off load the caching/resizing to an on demand model. so say your application is being run on device A, which requires 200x200 images. you'd change the image links to:

做一些有点不同的事情呢?基本上卸载缓存/调整大小到按需模型。所以说你的应用程序正在设备A上运行,它需要200x200的图像。你将图像链接更改为:

<img src="/images/image.php?height=200&width=200&source=filename.jpg" />

image.php could be a script which does the following:

image.php可以是执行以下操作的脚本:

  • make sure the existing file exists, and grab it from cache if it exists at this size
  • 确保现有文件存在,如果它以此大小存在,则从缓存中获取它

  • if not, resize the image and cache it
  • 如果没有,请调整图像大小并将其缓存

the next time your app looks for that image, it would be sent back at the 200px size. alternatively, if the app is now looking for a 300x300 image, that would be built/cached on the new request.

下次您的应用查找该图片时,它将以200px大小发回。或者,如果应用程序现在正在寻找300x300图像,那么将根据新请求构建/缓存该图像。

#2


0  

SEE Best way to cache resized images using PHP and MySQL
for really good ideas on caching mechanism (especially apache webserver intervention concept)

SEE使用PHP和MySQL缓存已调整大小的图像的最佳方法,以获得有关缓存机制的非常好的想法(尤其是apache webserver干预概念)

and

http://phpthumb.sourceforge.net/ which encapsulates using both/either ImageMagick or GD.

http://phpthumb.sourceforge.net/封装使用/ ImageMagick或GD。

#1


2  

what about doing something a bit different. basically off load the caching/resizing to an on demand model. so say your application is being run on device A, which requires 200x200 images. you'd change the image links to:

做一些有点不同的事情呢?基本上卸载缓存/调整大小到按需模型。所以说你的应用程序正在设备A上运行,它需要200x200的图像。你将图像链接更改为:

<img src="/images/image.php?height=200&width=200&source=filename.jpg" />

image.php could be a script which does the following:

image.php可以是执行以下操作的脚本:

  • make sure the existing file exists, and grab it from cache if it exists at this size
  • 确保现有文件存在,如果它以此大小存在,则从缓存中获取它

  • if not, resize the image and cache it
  • 如果没有,请调整图像大小并将其缓存

the next time your app looks for that image, it would be sent back at the 200px size. alternatively, if the app is now looking for a 300x300 image, that would be built/cached on the new request.

下次您的应用查找该图片时,它将以200px大小发回。或者,如果应用程序现在正在寻找300x300图像,那么将根据新请求构建/缓存该图像。

#2


0  

SEE Best way to cache resized images using PHP and MySQL
for really good ideas on caching mechanism (especially apache webserver intervention concept)

SEE使用PHP和MySQL缓存已调整大小的图像的最佳方法,以获得有关缓存机制的非常好的想法(尤其是apache webserver干预概念)

and

http://phpthumb.sourceforge.net/ which encapsulates using both/either ImageMagick or GD.

http://phpthumb.sourceforge.net/封装使用/ ImageMagick或GD。