在PHP中创建临时拇指图像

时间:2009-09-06 16:37:38

标签: php thumbnails temporary-files

嘿,我正在寻找一种在PHP中创建临时拇指文件的方法。有没有办法不将图像存储在服务器上或立即删除它们。我正在寻找的是这样的解决方案:http://www.dig2go.com/index.php?shopbilde=772&type=1&size=120

有人可以向我解释一下这背后的php代码是如何工作的吗?目前正在使用我在网上找到的用于创建拇指指甲的PHP代码:

function createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight,    $saveNameAndPath)
{
   if(!file_exists($pathToImage))
   return false;

   else
   {
      //Load image and size
      $img = imagecreatefromjpeg($pathToImage);
      $width = imagesx($img);
      $height = imagesy($img);

      //Calculate the size of thumb
      $new_width = $thumbWidth;
      $new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));

      //Make the new thumb
      $tmp_img = imagecreatetruecolor($new_width, $new_height);

      //Copy the old image and calculate the size
      imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

      //Save the thumb to jpg 
      imagejpeg($tmp_img, $saveNameAndPath);

      return true;
    }
}

2 个答案:

答案 0 :(得分:1)

function createThumb($ pathToImage,$ pathToThumb,$ thumbWidth,$ thumbHeight,$ saveNameAndPath)     {        如果(!file_exists($映像路径))        返回false;

   else
   {
      //Load image and size
      $img = imagecreatefromjpeg($pathToImage);
      $width = imagesx($img);
      $height = imagesy($img);

      //Calculate the size of thumb
      $new_width = $thumbWidth;
      $new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));

      //Make the new thumb
      $tmp_img = imagecreatetruecolor($new_width, $new_height);

      //Copy the old image and calculate the size
      imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
      // sets the header and creates the temporary thumbnail
      // ideal for ajax requests / <img> elements
      header("Content-type: image/jpeg");
      imagejpeg($img);

      return true;
    }
}

答案 1 :(得分:1)

你可以使用ini_set(“memory_limit”,“12M”);在脚本中设置内存限制。您可以将它从12兆字节扩展到最大内存。

一般来说64M是好的。