How to optimize Prestashop images

Speed matter, that’s simple fact. If you are looking for easy way how to optimize your image sizes under Prestashop you might consider this solution.

Buy Smush Optimize Module

http://www.presteamshop.com/en/modules-prestashop/smush-optimize-image.html

This module will optimize eshop pictures using Smush.it service, which is provided for free from Yahoo.

This optimalization is lossless and can save (from my experiences) about 10 – 20% without touching a single pixel. Tested on Prestashop 1.4

image

“Unfortunatelly” I’ve found, that Prestashop 1.6 does well with picture processing and there are no more savings (based on hunderds of files I tried to smush).

I did some research and ended up with Kraken.io service. This is pretty good but lacking some functions for mass processing (like upload pictures in ZIP format). The only option here is use their API. So I had to modify SmushIt class. Here is the code (class is available in /lib directory). Feel free to use.

(note: it might be little confusing keep SmushIt class name for Kraken.io service, but this way is much faster compare to create standalone Kraken class and than modify module code, because class is called from many places)

class SmushIt
{

    protected $auth = array();

    public function __construct($idata = null)
    {
        $this->auth = array(
            „auth“ => array(
                „api_key“ => „YOUR_API_GOES_HERE“,
                „api_secret“ => „YOUR_API_SECRET_GOES_HERE“
              )
        );

        if (!is_null($idata)) {
          if (preg_match(‚/https?:\/\//‘, $idata) == 1) {
              $params = array(
                    „url“ => $idata,
                    „wait“ => true,
                    „lossy“ => true
                    );
                    $data = $this->upload($params);
          } else {
               $params = array(
                    „file“ => $idata,
                    „wait“ => true,
                    „lossy“ => true
                    );
                    $data = $this->upload($params);
          }

       
            if ($data[„success“]) {
                $perct = round(((int)$data[„original_size“]-(int)$data[„kraked_size“]) / (int)$data[„original_size“] * 100, 2);
                $this->filename = substr(strrchr($idata, ‚/‘), 1);
            $this->size = $data[„original_size“];
            $this->compressedUrl = rawurldecode($data[„kraked_url“]);
            $this->compressedSize = $data[„kraked_size“];
            $this->savings = $data[„saved_bytes“].’b. ‚.$perct;
            return true;
               
                } else {
                    $this->error = $data[„error“];
              return false;
                }                           
        }       
    }

    public function url($opts = array())
    {
        $data = json_encode(array_merge($this->auth, $opts));
        $response = self::request($data, „https://api.kraken.io/v1/url“);

        return $response;
    }

    public function upload($opts = array())
    {
        if (!isset($opts[‚file‘]))
        {
            return array(
                „success“ => false,
                „error“ => „File parameter was not provided“
            );
        }

        if (preg_match(„/\/\//i“, $opts[‚file‘]))
        {
            $opts[‚url‘] = $opts[‚file‘];
            unset($opts[‚file‘]);
            return $this->url($opts);
        }

        if (!file_exists($opts[‚file‘]))
        {
            return array(
                „success“ => false,
                „error“ => „File `“ . $opts[‚file‘] . „` does not exist“
            );
        }

        if( class_exists( ‚CURLFile‘) ) {
            $file = new CURLFile( $opts[‚file‘] );
        } else {
            $file = ‚@‘ . $opts[‚file‘];
        }

        unset($opts[‚file‘]);

        $data = array_merge(array(
            „file“ => $file,
            „data“ => json_encode(array_merge(
                            $this->auth, $opts
            ))
        ));

        $response = self::request($data, „https://api.kraken.io/v1/upload“);

        return $response;
    }

    public function status()
    {
        $data = array(‚auth‘ => array(
            ‚api_key‘ => $this->auth[‚auth‘][‚api_key‘],
            ‚api_secret‘ => $this->auth[‚auth‘][‚api_secret‘]
        ));
        $response = self::request(json_encode($data), „https://api.kraken.io/user_status“);

        return $response;
    }

    private function request($data, $url)
    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_FAILONERROR, 1);

        $response = json_decode(curl_exec($curl), true);
        $error = curl_errno($curl);

        curl_close($curl);

        if ($error > 0) {
            throw new RuntimeException(sprintf(‚cURL returned with the following error code: „%s“‚, $error));
        }

        return $response;
    }

}

As you can see there is a

„lossy“ => true

option which will result to quality loss. You can remove it in order to get optimized pictures without pixels changes. But based on my tests visual quality difference is almost unnoticeable with this option and you can save a lot.

image

Go ahead and upload any test picture to Kraken.io and check how this compression works for you.

This entry was posted in Sčot. Bookmark the permalink.

Napsat komentář

Vaše e-mailová adresa nebude zveřejněna.