【问题标题】:How to change Image to jpg and set dpi to 300 using php?如何使用 php 将图像更改为 jpg 并将 dpi 设置为 300?
【发布时间】:2013-05-03 14:36:49
【问题描述】:

我在pngtojpgAction() 下的控制器中有以下代码,我使用ajax 调用它。

通过这个

$this->getRequest()->getParam('imagedata'));

声明我得到了这样的模式data:image/jpeg;base64,/9j/4AAQSkZJR......AH9T796KtUV1HGf/Z

这是一个png图像数据。

现在我正在使用以下代码将此 png 图像转换为 jpeg 并将 dpi 增加到 300。

public function pngtojpgAction()   
    {
                        //Code to convert png to jpg image
            $input = imagecreatefrompng($this->getRequest()->getParam('imagedata'));
            $width=imagesx($input);
            $height=imagesy($input);
            $output = imagecreatetruecolor($width, $height);
            $white = imagecolorallocate($output,  255, 255, 255);
            imagefilledrectangle($output, 0, 0, $width, $height, $white);
            imagecopy($output, $input, 0, 0, 0, 0, $width, $height);


            ob_start();
            imagejpeg($output);
            $contents =  ob_get_contents();
            ob_end_clean();
             //echo 'data:image/jpeg;base64,'.base64_encode($contents); /*Up to here code works well*/          

             $jpgImage='data:image/jpeg;base64,'.base64_encode($contents);


             $image = file_get_contents($jpgImage);

             $image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);

             header("Content-type: image/jpeg");
             header('Content-Disposition: attachment; filename="'.basename($jpgImage).'"');
             echo  $image;      

    }

使用这个

 $image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);

我想将图像的 dpi 增加到 300 dpi。

我无法使用这行代码更改图像 dpi

 $jpgImage='data:image/jpeg;base64,'.base64_encode($contents);


             $image = file_get_contents($jpgImage);

             $image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);

             header("Content-type: image/jpeg");
             header('Content-Disposition: attachment; filename="'.basename($jpgImage).'"');
             echo  $image;

我用这个链接作为参考Change image dpi using php

【问题讨论】:

标签: php magento gd


【解决方案1】:

做了一些更改后,它对我有用。

public function pngtojpgAction()   
{
            //Code to convert png to jpg image
        $input = imagecreatefrompng($this->getRequest()->getParam('imagedata'));
        $width=imagesx($input);
        $height=imagesy($input);
        $output = imagecreatetruecolor($width, $height);
        $white = imagecolorallocate($output,  255, 255, 255);
        imagefilledrectangle($output, 0, 0, $width, $height, $white);
        imagecopy($output, $input, 0, 0, 0, 0, $width, $height);                  

        ob_start();
        imagejpeg($output);
        $contents =  ob_get_contents();
        //Converting Image DPI to 300DPI                
        $contents = substr_replace($contents, pack("cnn", 1, 300, 300), 13, 5);             
        ob_end_clean();     
        echo 'data:image/jpeg;base64,'.base64_encode($contents); 

}

【讨论】:

  • 但我个人的经验表明,image magick 库更适合图像和 dpi 转换。与 GD 库相比,这提供了更好的质量。
  • 你能告诉我如何在 magento 网站中使用 imag magick 制作像 here 一样的产品页面
【解决方案2】:

我会改用 imagemagic:

convert Bird.jpg -density 300 Bird2.jpg

但你也可以用 GD 做到这一点。

Link to class

$filename = 'Bird.jpg';
$source = imagecreatefromjpeg($filename);
list($width, $height) = getimagesize($filename);

$b = new Resampler;
$im = $b->resample($source, $height, $width, 300);

file_put_contents('Bird2.jpg', $im);

在 Windows 环境下测试。

【讨论】:

    【解决方案3】:

    重新生成质量可调的图像的简单代码。

    function compress_image($source_url, $destination_url, $quality) {
        $info = getimagesize($source_url);
        $image = imagecreatefromjpeg($source_url);
        imagejpeg($image, $destination_url, $quality);
        return $destination_url;
    }
    echo compress_image($_FILES["file"]["tmp_name"], "destination .jpg", 80);//Adjust Quality
    

    注意:确保安装了 PHP 的 GD 库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      相关资源
      最近更新 更多