使用 PHP 创建图像缩略图

我为客户创建了很多照片库。网站是一种营销工具,还有什么比显示产品的大量图片更好的营销方式呢?经常出现的一个问题是客户无法创建自己的缩略图。这个问题因他们不想付钱让我创建它们而变得更加复杂。妥协是我创建的 PHP 缩略图生成函数。

PHP

function make_thumb($src, $dest, $desired_width) {

	/* read the source image */
	$source_image = imagecreatefromjpeg($src);
	$width = imagesx($source_image);
	$height = imagesy($source_image);
	
	/* find the "desired height" of this thumbnail, relative to the desired width  */
	$desired_height = floor($height * ($desired_width / $width));
	
	/* create a new, "virtual" image */
	$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
	
	/* copy source image at a resized size */
	imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
	
	/* create the physical thumbnail image to its destination */
	imagejpeg($virtual_image, $dest);
}

以上代码使用了 PHP GD2 库功能。使用 PHP 创建图像的唯一缺点是缩略图看起来不如在 Photoshop 或 GIMP 中创建的缩略图好。

你会用这个吗?建议?分享它们!

赞(0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏