【问题标题】:ImageMagick get web-optimized picturesImageMagick 获取网络优化图片
【发布时间】:2012-05-19 08:56:26
【问题描述】:

我正在使用 ImageMagick 获取上传图片的 3 个版本。

问题在于这 3 张新图片并未像 Photoshop 中的 Save For Web & Devices 那样针对 Web 进行优化。差别很大。 ImageMagick 生成的文件比 Photoshop 中的网络优化图片大 5 倍。

你能帮我改善这个问题吗?

这是 ImageMagick 部分:

exec($imageMagickPath." uploads/foto.jpg -resize 514x uploads/fl-foto.jpg");
exec($imageMagickPath." uploads/fl-foto.jpg -resize 320x320 uploads/hl-foto.jpg");
exec($imageMagickPath." uploads/fl-foto.jpg -resize x96 -gravity center -crop 96x96+0+0 +repage uploads/th-foto.jpg");

当然,我想在前端使用这3张图片,但是它们会变大并减慢整个页面的速度。

有什么建议吗?

【问题讨论】:

    标签: php imagemagick


    【解决方案1】:

    Imagemagick 使用参数-quality # 来设置所需的质量,通过它您可以通过增加压缩来减小图像大小。您可以在他们的documentation 上阅读更多相关信息。关于 jpeg 的部分是这样说的:

    对于 JPEG 和 MPEG 图像格式,质量为 1(最低图像 质量和最高压缩率)到 100(质量最好但最少 有效压缩)。默认是使用估计的质量 你的输入图像是否可以确定,否则 92. 当 质量大于 90,则色度通道不 下采样。使用 -sampling-factor 选项指定因子 用于色度下采样。

    在你的情况下,这将变成:

    exec($imageMagickPath." uploads/foto.jpg -resize 514x -quality 60 uploads/fl-foto.jpg");
    

    【讨论】:

    • 谢谢。在转换为 sRGB 后,我还剥离了元信息和配置文件。现在我认为它和Photoshop一样。我添加了“-profile”.$profilepath.“sRGB.icc -strip”
    • @John Doe Smith:你不仅应该'accept'最好的,而且'upvote'其他有帮助的答案。当然,接受的答案通常也值得一票。只需点击答案左侧的小^ 图标...
    【解决方案2】:

    你能试试这个代码吗?

       $im = new Imagick('Penguins.jpg');
       $im->cropImage(300, 300, 0, 0);       
       header("Content-Type: image/png");
       echo $im;
    

    如果它不工作。你介意我给你的phpinfo吗?

      <?php phpinfo(); ?>
    

    【讨论】:

      【解决方案3】:
      <p>
      //Function for image re-size and cropping//
      if(isset($_POST))
      {
           //Some Settings
          $ThumbSquareSize        = 200; //Thumbnail will be 200x200
          $BigImageMaxSize        = 500; //Image Maximum height or width
          $ThumbPrefix            = "thumb_"; //Normal thumb Prefix
          $DestinationDirectory   = 'uploads/'; //Upload Directory ends with / (slash)
          $Quality                = 90;
      
          // check $_FILES['ImageFile'] array is not empty
          // "is_uploaded_file" Tells whether the file was uploaded via HTTP POST
          if(!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name']))
          {
                  die('Something went wrong with Upload!'); // output error when above checks fail.
          }
      
          // Random number for both file, will be added after image name
          $RandomNumber   = rand(0, 9999999999); 
      
          // Elements (values) of $_FILES['ImageFile'] array
          //let's access these values by using their index position
          $ImageName      = str_replace(' ','-',strtolower($_FILES['ImageFile']['name'])); 
          $ImageSize      = $_FILES['ImageFile']['size']; // Obtain original image size
          $TempSrc        = $_FILES['ImageFile']['tmp_name']; // Tmp name of image file stored in PHP tmp folder
          $ImageType      = $_FILES['ImageFile']['type']; //Obtain file type, returns "image/png", image/jpeg, text/plain etc.
      
          switch(strtolower($ImageType))
          {
              case 'image/png':
                  $CreatedImage =  imagecreatefrompng($_FILES['ImageFile']['tmp_name']);
                  break;
              case 'image/gif':
                  $CreatedImage =  imagecreatefromgif($_FILES['ImageFile']['tmp_name']);
                  break;          
              case 'image/jpeg':
              case 'image/pjpeg':
                  $CreatedImage = imagecreatefromjpeg($_FILES['ImageFile']['tmp_name']);
                  break;
              default:
                  die('Unsupported File!'); //output error and exit
          }
      
          //PHP getimagesize() function returns height-width from image file stored in PHP tmp folder.
          //Let's get first two values from image, width and height. list assign values to $CurWidth,$CurHeight
          list($CurWidth,$CurHeight)=getimagesize($TempSrc);
          //Get file extension from Image name, this will be re-added after random name
          $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
          $ImageExt = str_replace('.','',$ImageExt);
      
          //remove extension from filename
          $ImageName      = preg_replace("/\\.[^.\\s]{3,4}$/", "", $ImageName); 
      
          //Construct a new image name (with random number added) for our new image.
          $NewImageName = $ImageName.'-'.$RandomNumber.'.'.$ImageExt;
          //set the Destination Image
          $thumb_DestRandImageName    = $DestinationDirectory.$ThumbPrefix.$NewImageName; //Thumb name
          $DestRandImageName          = $DestinationDirectory.$NewImageName; //Name for Big Image
      
          //Resize image to our Specified Size by calling resizeImage function.
          if(resizeImage($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType))
          {
              //Create a square Thumbnail right after, this time we are using cropImage() function
              if(!cropImage($CurWidth,$CurHeight,$ThumbSquareSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType))
                  {
                      echo 'Error Creating thumbnail';
                  }
              /*
              At this point we have succesfully resized and created thumbnail image
              We can render image to user's browser or store information in the database
              For demo, we are going to output results on browser.
              */
              echo '<table width="100%" border="0" cellpadding="4" cellspacing="0">';
              echo '<tr>';
              echo '<td align="center"><img src="uploads/'.$ThumbPrefix.$NewImageName.'" alt="Thumbnail"></td>';
              echo '</tr><tr>';
              echo '<td align="center"><img src="uploads/'.$NewImageName.'" alt="Resized Image"></td>';
              echo '</tr>';
              echo '</table>';
      
              /*
              // Insert info into database table!
              mysql_query("INSERT INTO myImageTable (ImageName, ThumbName, ImgPath)
              VALUES ($DestRandImageName, $thumb_DestRandImageName, 'uploads/')");
              */
      
          }else{
              die('Resize Error'); //output error
          }
      }
      
      
      // This function will proportionally resize image 
      function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
      {
          //Check Image size is not 0
          if($CurWidth <= 0 || $CurHeight <= 0) 
          {
              return false;
          }
      
          //Construct a proportional size of new image
          $ImageScale         = min($MaxSize/$CurWidth, $MaxSize/$CurHeight); 
          $NewWidth           = ceil($ImageScale*$CurWidth);
          $NewHeight          = ceil($ImageScale*$CurHeight);
          $NewCanves          = imagecreatetruecolor($NewWidth, $NewHeight);
      
          // Resize Image
          if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
          {
              switch(strtolower($ImageType))
              {
                  case 'image/png':
                      imagepng($NewCanves,$DestFolder);
                      break;
                  case 'image/gif':
                      imagegif($NewCanves,$DestFolder);
                      break;          
                  case 'image/jpeg':
                  case 'image/pjpeg':
                      imagejpeg($NewCanves,$DestFolder,$Quality);
                      break;
                  default:
                      return false;
              }
          //Destroy image, frees memory   
          if(is_resource($NewCanves)) {imagedestroy($NewCanves);} 
          return true;
          }
      
      }
      
      //This function corps image to create exact square images, no matter what its original size!
      function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType)
      {    
          //Check Image size is not 0
          if($CurWidth <= 0 || $CurHeight <= 0) 
          {
              return false;
          }
      
          if($CurWidth>$CurHeight)
          {
              $y_offset = 0;
              $x_offset = ($CurWidth - $CurHeight) / 2;
              $square_size    = $CurWidth - ($x_offset * 2);
          }else{
              $x_offset = 0;
              $y_offset = ($CurHeight - $CurWidth) / 2;
              $square_size = $CurHeight - ($y_offset * 2);
          }
      
          $NewCanves  = imagecreatetruecolor($iSize, $iSize); 
          if(imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size))
          {
              switch(strtolower($ImageType))
              {
                  case 'image/png':
                      imagepng($NewCanves,$DestFolder);
                      break;
                  case 'image/gif':
                      imagegif($NewCanves,$DestFolder);
                      break;          
                  case 'image/jpeg':
                  case 'image/pjpeg':
                      imagejpeg($NewCanves,$DestFolder,$Quality);
                      break;
                  default:
                      return false;
              }
          //Destroy image, frees memory   
          if(is_resource($NewCanves)) {imagedestroy($NewCanves);} 
          return true;
      
          }
      
      }
      </p>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-08
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 1970-01-01
        • 2023-04-04
        相关资源
        最近更新 更多