在 PHP 中创建临时拇指图像

发布于 2024-08-03 19:49:39 字数 1139 浏览 2 评论 0原文

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

Hey, i am looking for a method to creat temporay thumb file in PHP. Is there any way not to store the image on the server or delete them right after. What I am looking for is a solution like this: http://www.dig2go.com/index.php?shopbilde=772&type=1&size=120

Could some one explain to me how the php code behind this works? For the moment are am using a php code I found on the net for creation of thumb nails:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾其所爱 2024-08-10 19:49:39

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

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);
      // sets the header and creates the temporary thumbnail
      // ideal for ajax requests / <img> elements
      header("Content-type: image/jpeg");
      imagejpeg($img);

      return true;
    }
}
撩发小公举 2024-08-10 19:49:39

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

一般64M就不错了。

You can use ini_set("memory_limit","12M"); to set memory limit in your script. You can extend it from 12 megabytes to maximum memory you have.

Generally 64M is good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文