【问题标题】:PHP Save Image After imagecopyresampledPHP在imagecopyresampled后保存图像
【发布时间】:2014-02-20 19:51:34
【问题描述】:
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

如何将调整大小的图像保存到文件夹/? 以及如何检测图像类型是 jpg/png/gif?

【问题讨论】:

    标签: php image


    【解决方案1】:

    要将图像保存到文件中,您可以使用以下任意一种:imagejpeg()imagepng()imagegif(),具体取决于您所需的输出格式。

    要检测图像类型,您只需检查文件的扩展名并以此为基础。但是,有时人们手动更改图像文件的扩展名,认为实际上更改了图像类型,因此检查 imagecreatefrom 是否返回图像资源而不是 false 总是一个好主意。

    要快速返回文件的扩展名:

    $ext = pathinfo($path_to_file, PATHINFO_EXTENSION);
    

    Manual entry on pathinfo()

    【讨论】:

      【解决方案2】:

      添加此代码

      imagepng($iOut,'pic/mypic.png',3);
      

      &此代码从外部来源获取您的图片格式

      $link='http://example.com/example.png';
      echo (substr ($link,strrpos ($link,".")+1));
      

      【讨论】:

        【解决方案3】:

        您可以定义任何类型的图像:

         // Save the image as 'simpletext.jpg'
         imagejpeg($im, 'path/to/your/image.jpg');
         // or another image
         imagepng($im, 'path/to/your/image.png');
        

        在此处查看示例http://php.net/manual/en/function.imagecopyresampled.php

        【讨论】:

          【解决方案4】:
          $filename = 'path/to/original/file.xxx'; // where xxx is file type (jpg, gif, or png)
          $newfilename = 'path/to/resized/file.xxx'; // where xxx is file type (jpg, gif, or png)
          $path_parts = pathinfo($filename);
          if ($path_parts['extension'] == 'jpg') {
              $image_p = imagecreatetruecolor($new_width, $new_height);
              $image = imagecreatefromjpeg($filename);
              imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
              imagejpeg($image_p, $newfilename);
          } elseif ($path_parts['extension'] == 'gif') {
              $image_p = imagecreatetruecolor($new_width, $new_height);
              $image = imagecreatefromgif($filename);
              imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
              imagegif($image_p, $newfilename);
          } elseif ($path_parts['extension'] == 'png') {
              $image_p = imagecreatetruecolor($new_width, $new_height);
              $image = imagecreatefrompng($filename);
              imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
              imagepng($image_p, $newfilename);
          } else {
                  echo "Source file is not a supported image file type.";
          }
          

          【讨论】:

          • 它的工作正常只是用PNG文件创建问题,因为重新生成的文件需要是透明的。如何做到这一点。
          【解决方案5】:

          应用imagecopyresampled() 后,$dst_image 将成为您的图像资源标识符。

          仅应用 imagecopyresampled() 函数不会自动将其保存到文件系统中。

          所以您需要使用imagejpeg()imagepng() 中的一个函数来保存它

          // Output
          imagejpeg($dst_image, 'new-image.jpg', 100);
          

          【讨论】:

            【解决方案6】:

            将图像保存为 jpg 参见 imagejpeg 函数

            http://nz.php.net/manual/en/function.imagejpeg.php

            获取图片扩展使用

            $path_parts = pathinfo($filename);
            echo $path_parts['extension'];
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-06-25
              • 1970-01-01
              • 1970-01-01
              • 2013-03-02
              • 2010-09-07
              • 2013-02-28
              • 2023-03-30
              相关资源
              最近更新 更多