我有一些从服务器提取的图像$imgUrl
保存图像的路径。
现在我用<img src="<?php echo $imgUrl ?>" width="100" height="200"/>
或 CSS 来缩小图像,但我想在 PHP 中执行此操作,以便将已缩放的图像提供给 DOM
有任何想法吗?
Thanks
此解决方案将导致在第一次请求时创建拇指。所有未来的请求都将获取已经创建的拇指。它正在使用图像魔术师 http://php.net/manual/en/book.imagick.php:
HTML:
<img src="script.php?img=example" />
PHP(脚本.php):
$width = 140;
$height = 80;
$image = $_GET['img'];
$ext = 'png';
// Check if file exists
if ( ! file_exists('/path/to/the/'.$image.'.'.$ext))
{
die('Unable to process the requested file.');
}
// Check if a thumb already exists, otherwise create a thumb
if (file_exists('/path/to/the/'.$image.'_thumb.'.$ext))
{
$img = new imagick('/path/to/the/'.$image.'_thumb.'.$ext);
}
else
{
$img = new imagick('/path/to/the/'.$image.'.'.$ext);
$img->setImageFormat($ext);
$img->scaleImage($width, 0);
$img->cropImage($width, $height, 0, 0);
$img->writeImage('/path/to/the/'.$image.'_thumb.'.$ext);
}
// Return as an image
header('Content-Type: image/'.$ext);
echo $img;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)