【问题标题】:Creat temporay thumb image in PHP在 PHP 中创建临时拇指图像
【发布时间】:2013-12-11 20:22:26
【问题描述】:

嘿,我正在寻找一种在 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;
    }
}

【问题讨论】:

  • 我知道这是旧的。只是想知道答案是否好。加上一些使用提示。欢呼
  • 它实际上非常有帮助,我记得它让它起作用了。但是我使用的代码我不知道去哪里了。你有什么问题? :)

标签: php thumbnails temporary-files


【解决方案1】:

函数 createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight, $saveNameAndPath) { if(!file_exists($pathToImage)) 返回错误;

   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;
    }
}

【讨论】:

    【解决方案2】:

    你可以使用 ini_set("memory_limit","12M");在脚本中设置内存限制。您可以将其从 12 MB 扩展到您拥有的最大内存。

    一般64M就不错了。

    【讨论】:

      猜你喜欢
      • 2012-03-23
      • 2011-02-06
      • 2014-11-06
      • 2014-12-23
      • 2012-09-23
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      相关资源
      最近更新 更多