首页 > 解决方案 > php中图像的缩略图生成问题

问题描述

我编写了以下代码来为 php 中的图像生成缩略图,它对某些图像工作正常,但在高分辨率/高尺寸图像的情况下它显示

此页面无法正常工作

问题。这里imagecreatefromjpeg()不工作。有什么解决办法请帮帮我..

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

/* read the source image */
//ini_set('gd.jpeg_ignore_warning', 1);
//echo $src;exit;
//echo $src;exit;
$source_image = @imagecreatefromjpeg($src);
echo $src;exit;
if (!$source_image)
{
  $source_image= @imagecreatefromstring(file_get_contents($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 */
@imageCopyResized($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

/* create the physical thumbnail image to its destination */
@header('Content-Type: image/jpeg');
@imagejpeg($virtual_image, $dest);

}

标签: phplaravelzend-framework

解决方案


如果您曾经在 PHP 应用程序中进行过任何类型的图像处理,您就会开始意识到使用本地 PHP 命令(如 createimagefromjpg 等)时的局限性。它会占用您的 Web 服务器内存!如今,人们在手机中携带 10 兆像素的摄像头,上传和调整照片大小可能对资源造成真正的压力,尤其是当网站上的多个用户同时进行此操作时。

为了解决这个难题,有一个名为 imagick 的 PHP 库(一个包装类),它允许您访问一个名为 ImageMagick 的终端程序。ImageMagick 在机器上本地运行,可用于 Unix、Linux、Mac 和 Windows,因此运行它应该没有问题。这里唯一要考虑的是您的托管服务提供商 PHP 是否有可用的 imagick。如果没有,根据您的托管包,您可能能够通过 SSH 连接到您的服务器并安装它。

一旦我切换到使用 IMagick PHP 类,错误就停止了,网站的速度也大大加快了。

以下是在 Linux 和 Windows 上安装的方法:

如何:在 Ubuntu 11.10 上安装 Imagick(用于 php)

如何在 Windows 7 上安装 ImageMagick 以与 PHP 一起使用 (3)

这是 IMagick 类的文档:http: //be2.php.net/manual/en/class.imagick.php


推荐阅读