【问题标题】:Changing image width when sending via header in php通过 php 中的标头发送时更改图像宽度
【发布时间】:2010-10-16 22:17:53
【问题描述】:

我有这段代码调用随机图像并将其发送到另一个页面。该页面通过图像链接调用此脚本。我想弄清楚的是如何在图像超过我的最大宽度时指定图像宽度。我走了这么远,然后完全迷路了。

 <?php

    $folder = '';

    $exts = 'jpg jpeg png gif';

    $files = array(); $i = -1; 
    if ('' == $folder) $folder = './';

    $handle = opendir($folder);
    $exts = explode(' ', $exts);
    while (false !== ($file = readdir($handle))) {
    foreach($exts as $ext) { 
    if (preg_match('/\.'.$ext.'$/i', $file, $test)) { 
    $files[] = $file; 
    ++$i;
    }
    }
    }
    closedir($handle);
    mt_srand((double)microtime()*1000000); 
    $rand = mt_rand(0, $i); 

    $image =($folder.$files[$rand]); 

    if (file_exists($image))
    {
      list($width) = getimagesize($image);
      $maxWidth = 150;
      if ($width > $maxWidth)
      {
        header('Location: '.$folder.$files[$rand]); // Voila!;
      }
      else
      {
        header('Location: '.$folder.$files[$rand]); // Voila!
      }
    }
    ?>

【问题讨论】:

    标签: php image


    【解决方案1】:

    您可以使用 CSS max-widthmax-height 属性。

    【讨论】:

      【解决方案2】:

      这里有几个调整大小的函数,我认为其中至少有一个可以保持纵横比。

      function resize_image($file, $w, $h, $crop = false) {
          list($width, $height) = getimagesize($file);
          $r = $width / $height;
          if ($crop) {
              if ($width > $height) {
                  $width = ceil($width-($width*($r-$w/$h)));
              } else {
                  $height = ceil($height-($height*($r-$w/$h)));
              }
              $newwidth = $w;
              $newheight = $h;
          } else {
              if ($w/$h > $r) {
                  $newwidth = $h*$r;
                  $newheight = $h;
              } else {
                  $newheight = $w/$r;
                  $newwidth = $w;
              }
          }
          $src = imagecreatefromjpeg($file);
          $dst = imagecreatetruecolor($newwidth, $newheight);
          imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
      
          return $dst;
      }
      

      还有:

      function thumb_aspect_crop($source_path, $desired_width, $desired_height) {
          list( $source_width, $source_height, $source_type ) = getimagesize($source_path);
      
          switch ($source_type) {
              case IMAGETYPE_GIF:
                  $source_gdim = imagecreatefromgif($source_path);
                  break;
      
              case IMAGETYPE_JPEG:
                  $source_gdim = imagecreatefromjpeg($source_path);
                  break;
      
              case IMAGETYPE_PNG:
                  $source_gdim = imagecreatefrompng($source_path);
                  break;
          }
      
          $source_aspect_ratio = $source_width / $source_height;
          $desired_aspect_ratio = $desired_width / $desired_height;
      
          if ($source_aspect_ratio > $desired_aspect_ratio) {
              // Triggered when source image is wider
              $temp_height = $desired_height;
              $temp_width = (int) ( $desired_height * $source_aspect_ratio );
          } else {
              // Triggered otherwise (i.e. source image is similar or taller)
              $temp_width = $desired_width;
              $temp_height = (int) ( $desired_width / $source_aspect_ratio );
          }
      
          // Resize the image into a temporary GD image
          $temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
          imagecopyresampled(
                  $temp_gdim,
                  $source_gdim,
                  0, 0,
                  0, 0,
                  $temp_width, $temp_height,
                  $source_width, $source_height
          );
      
          // Copy cropped region from temporary image into the desired GD image
          $x0 = ( $temp_width - $desired_width ) / 2;
          $y0 = ( $temp_height - $desired_height ) / 2;
      
          $desired_gdim = imagecreatetruecolor($desired_width, $desired_height);
          imagecopy(
                  $desired_gdim,
                  $temp_gdim,
                  0, 0,
                  $x0, $y0,
                  $desired_width, $desired_height
          );
      
          return $desired_gdim;
      }
      

      【讨论】:

        【解决方案3】:

        尝试使用Primage class,比如example

        【讨论】:

          【解决方案4】:

          您不能在标题中指定。有两种方法可以解决。

          1:在图片标签中,指定宽度属性,但是这会强制较小的图片放大,所以可能不是最好的做法。

          2 : 使用 GD 库动态调整图像大小并发送结果图像而不是原始图像。

          编辑:

          对于调整大小,您可以查看这个非常简单易用的图像调整大小类。 http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

          include('SimpleImage.php');
          $image = new SimpleImage();
          $image->load('picture.jpg');
          $image->resizeToWidth($maxWidth);
          $image->save('picture2.jpg');
          

          现在您可以重定向到picture2.jpg 或内联发送图像内容。此外,如果图像经常使用,则检查脚本顶部,如果您调整大小的图像存在,如果它确实发送它,否则继续调整大小。

          【讨论】:

          • 废话 - 我用 gd 找到的每个调整大小的脚本都要求您提前知道您想要图像的高度/宽度。我只是希望它不超过某个宽度,如果它大于那个宽度,那么让它调整到任何使它保持在小于那个宽度的大小。它还能像现在一样将调整大小的图像作为标题返回吗?
          • @Josepth Vodary ...看看我更新的答案,该类也允许您仅根据宽度调整大小。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-02-11
          • 2020-04-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多