2013-05-03 284 views
2

我有一个控制器下面的代码pngtojpgAction()我打电话使用ajax。如何将图像更改为jpg并使用php将dpi设置为300?

通过这个

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

声明我越来越喜欢这个data:image/jpeg;base64,/9j/4AAQSkZJR......AH9T796KtUV1HGf/Z

这是一个PNG图像数据的模式。

我现在用下面的代码到这个PNG图像转换为JPEG和使用该

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

我想提高图像的DPI为300 dpi增加的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;  

    } 

我无法使用这些代码行来改变图像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

+0

什么意思是“无法做到这一点”?有错误吗? – Tim 2013-05-03 14:43:40

+0

@Tim我无法将图像转换为300dpi – Muk 2013-05-03 14:46:19

+0

那么你得到一个有效的jpeg? – Tim 2013-05-03 14:49:14

回答

4

做了一些改变后,它为我工作。

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); 

} 
+1

但我个人的经验表明,图像magick库更适合图像和dpi转换。与GD库相比,这提供了更好的质量。 – Muk 2013-08-02 11:20:11

+0

可以请你告诉我如何在magento网站上使用imag magick来制作产品页面,就像[这里]一样(http://www.dailyobjects.com/custom-cases/apple/iphone-4-4s/custom-iphone -4-4s情况/苗条) – fresher 2016-07-18 12:12:43

3

我会用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环境。

相关问题