【问题标题】:How to use thumb generator inside PHP script如何在 PHP 脚本中使用拇指生成器
【发布时间】:2012-03-10 02:18:24
【问题描述】:

我正在尝试使用this 缩略图生成器,但如您所见,您必须通过this link 来创建图像缩略图。这可能是一个愚蠢的问题,但我如何让它在将一些变量保存到数据库的 php 脚本中工作?我试图包括

header("Location: http://www.zubrag.com/thumb.php?src=http://www.test.com/test.jpg&dest=thumb.jpg&x=100&y=50"); 

但它不起作用。我敢打赌,确实有很简单的解决方案,但我找不到。

【问题讨论】:

    标签: php thumbnails


    【解决方案1】:

    我认为您误读了说明。它不是 Web 服务,而是您必须安装到服务器的 PHP 脚本。 zubrag.com 网址只是一个示例,您可以将其替换为您自己网站的网址。

    【讨论】:

    • 我在这里举了一个例子,我已经把这个脚本上传到我的服务器上,它可以工作,但只能通过链接,我想在不将链接写入浏览器的情况下使用它。
    • 怎么用?您想向用户显示图像吗?保存到数据库?修改它?通过电子邮件发送?
    • 我希望它从文件中创建缩略图 :) 这就是这个脚本所做的,也是我想要做的,但在另一个 php 脚本中不是通过在浏览器中编写链接。
    【解决方案2】:

    如果没有任何个人先前使用特定生成器的经验,您需要以以下形式显示图像:

    <img src="thumb.php?src=link/to/image.jpg&x=100&y=50&f=0"/>
    

    【讨论】:

      【解决方案3】:

      我真的看不出你的问题...

      除了在调用脚本的那一行竖起大拇指之外,您还想做更多的事情吗?只需从 thumb.php 中取出代码行并在您的脚本中使用它。

      include('image.class.php');
      
      $img = new Zubrag_image;
      
      // initialize
      $img->max_x        = $max_x;
      $img->max_y        = $max_y;
      $img->cut_x        = $cut_x;
      $img->cut_y        = $cut_y;
      $img->quality      = $image_quality;
      $img->save_to_file = $save_to_file;
      $img->image_type   = $image_type;
      
      // generate thumbnail
      $img->GenerateThumbFile($images_folder . $from_name, $thumbs_folder . $to_name);
      

      您只需将值更改为所需的值...这应该在我对脚本的简短回顾中起作用。

      【讨论】:

      • 谢谢,我正在考虑这个解决方案,但我认为可能还有其他问题,但这会很好。
      【解决方案4】:

      你可以试试这个

      <?
      
      $property_id = 1; // for our little example here
      
      define("_IMAGE_PATH","property_images/");
      // max dimensions allowed:
      define("_IMAGE_WIDTH","640");
      define("_IMAGE_HEIGHT","480");
      define("_IMAGE_THUMB_WIDTH","100");
      define("_IMAGE_THUMB_HEIGHT","75");
      
      
      // grab the path to the temporary file (image) that the user uploaded
      $photo = $_FILES['new_image']['tmp_name'];
      // check if it exists
      if(is_uploaded_file($photo)){
          //the real image file name
          $real_name = strtolower($_FILES['new_image']['name']);
          // image type based on image file name extension:
          if(strstr($real_name,".png")){
              $image_type = "png";
          }else if(strstr($real_name,".jpg")){
              $image_type = "jpg";
          }else if(strstr($real_name,".gif")){
              $image_type = "gif";
          }else{
              die("Unsupported image type");
          }
      
          // find the next image name we are going to save
          $x=1;
          while(true){
              $image_name = _IMAGE_PATH."${property_id}/${x}.jpg";
              if(!is_file($image_name))break;
              $x++;
          }
      
          // start processing the main bigger image:
          $max_width = _IMAGE_WIDTH; $max_height = _IMAGE_HEIGHT;
          $size = getimagesize($photo);
          $width = $size[0];
          $height = $size[1];
          $x_ratio = $max_width / $width;
          $y_ratio = $max_height / $height;
          if(($width <= $max_width)&&($height <= $max_height)){
              $tn_width = $width;
              $tn_height = $height;
          }else{
              if(($x_ratio * $height) < $max_height){
                  $tn_height = ceil($x_ratio * $height);
                  $tn_width = $max_width;
              }else{
                  $tn_width = ceil($y_ratio * $width);
                  $tn_height = $max_height;
              }
          }
          switch($image_type){
              case "png": $src=imagecreatefrompng($photo); break;
              case "jpg": $src=imagecreatefromjpeg($photo); break;
              case "gif": $src=imagecreatefromgif($photo); break;
          }
          // destination resized image:
          $dst = imagecreatetruecolor($tn_width, $tn_height);
          // resize original image onto $dst
          imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
          // write the final jpeg image..
          imagejpeg($dst, $image_name, 100) or die("Error: your photo
          has not been saved. Please contact the administrator");
          // time to clean up
          imagedestroy($src);
          imagedestroy($dst);
      
      
          // and now we do it alll again for the thumbnail:
          $max_width = _IMAGE_THUMB_WIDTH; $max_height = _IMAGE_THUMB_HEIGHT;
          $size = GetImageSize($photo);
          $width = $size[0];
          $height = $size[1];
          $x_ratio = $max_width / $width;
          $y_ratio = $max_height / $height;
          if(($width <= $max_width)&&($height <= $max_height)){
              $tn_width = $width;
              $tn_height = $height;
          }else{
              if(($x_ratio * $height) < $max_height){
                  $tn_height = ceil($x_ratio * $height);
                  $tn_width = $max_width;
              }else{
                  $tn_width = ceil($y_ratio * $width);
                  $tn_height = $max_height;
              }
          }
          switch($image_type){
              case "png": $src=imagecreatefrompng($photo); break;
              case "jpg": $src=imagecreatefromjpeg($photo); break;
              case "gif": $src=imagecreatefromgif($photo); break;
          }
          $dst = imagecreatetruecolor($tn_width, $tn_height);
          imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
          $thumbfile = $image_name . ".thumb.jpg";
          if(file_exists($thumbfile))unlink($thumbfile);
          imagejpeg($dst, $thumbfile, 100) or die("Error: your photo thumb has not been saved.
             Please contact the administrator");
          imagedestroy($src);
          imagedestroy($dst);
      }
      ?> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-30
        • 1970-01-01
        • 2017-05-15
        • 1970-01-01
        • 2021-09-10
        • 2013-10-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多