0

I am working on magento 1.9 installation. I need to optimize images before/at the time of page loading. I have refered so many php scripts for this, http://zenverse.net/php-reducing-image-filesize-using-gd/ - I found this much better option. But it cannot create image from the image url. The error log says:

ERR (3): Warning: imagejpeg(): Unable to open 'http://mydomain/media/catalog/product/cache/3/small_image/180x180/9df785d08d6e5fb8d27136e95/4/8461c5c67fab50b68781772a4df.jpg' for writing: No such file or directory.

The function compress_image($source_url, $destination_url, $quality) works properly for local images. I searched on net for the issue, it says I need to pass relative url. I tried so many times but noting worked. My code in list.phtml:

<?php /* Display optimised images */
    $img = $this->helper('catalog/image')->init($_product, 'small_image')->resize($imgX, $imgY);
    $imgsrc = Mage::helper('featured')->compress_image($img, $img, 90);
    ?>
    <img src="<?php echo $imgsrc; ?> " width="<?php echo $imgX; ?>" height="  <?php echo $imgY; ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>

I dont know what I am missing. Thanks in advance.

Elangovan
  • 3,469
  • 4
  • 31
  • 38
Jhanvee S
  • 9
  • 1
  • 6
  • just helper('catalog/image')->init($_product, 'small_image')->resize($imgX, $imgY); ?> – sr3z Sep 01 '16 at 14:10
  • But Google Page Speed Insights still ask to optimize the image. – Jhanvee S Sep 01 '16 at 14:15
  • for google you can set height and width not need to compress it. – Sunny Rathod Sep 01 '16 at 14:20
  • could you post the value of $imgsrc and directory for your image files? It could be that you need to move compressed files from $imgsrc to image directory – sr3z Sep 01 '16 at 14:21
  • The function called is: public function compress_image($source_url, $destination_url, $quality) { $info = getimagesize($source_url); if ($info['mime'] == 'image/jpeg') { $image = imagecreatefromjpeg($source_url); imagejpeg($image, $destination_url, $quality); } elseif ($info['mime'] == 'image/png') { $image = imagecreatefrompng($source_url); imagepng($image, $destination_url, $quality); } //return destination file url return $destination_url; } But the line :$image = imagecreatefromjpeg($source_url); causes error. – Jhanvee S Sep 01 '16 at 14:29
  • ok, and what is $source_url ? Am I right it is $img variable in compress_image($img, $img, 90) invocation ? – sr3z Sep 01 '16 at 14:38
  • I would recommend you to read this post as well http://stackoverflow.com/questions/8711253/how-to-resize-the-categories-images-in-magento – sr3z Sep 01 '16 at 14:50
  • @sr3z: Yes. Its the $img variable passed to the function. – Jhanvee S Sep 01 '16 at 14:56

0 Answers0