Yii 1.1: Create image thumbnails with php_img_preview

9 followers

This is another way to view images in your application and I like this way because it's simple and I think it's better performance than other.

php_img_preview is great tool created by @muayyadalsad owner of Ojuba (Linux distribution), and here is the features:

Features:

  • support crop images.
  • support adjusts qaulity.
  • support aspect handling.
  • support watermark.
  • support JPG/PNG image types.
  • support php images filters like (grayscale)

How it work ?

when you request to image url this tool will create thumbnail and show it directly if not created before

original image: /images/myfolder/myimage.jpg

when you request $model->img_preview(150,150)

will create thumbnail at if not created: /images/preview/150x150/myfolder/myimage.jpg


Usage

1) Download php_img_preview

2) Download Img class

3) extract php_img_preview to images folder like this:

/images/preview/

-> docs/..

-> .htaccess

-> index.php

-> settings.php.in rename to (settings.php)

and add your custom sizes in sittings.php

public static $sizes = array(
    "75x110"=>true,
  )

4) create lib folder and exreact Img.zip here:

/protected/lib/Img.php

5) add lib to config/main

'import'=>array(
        'application.lib.*',
....
    ),

6) in model:

public function img_preview($w=false, $h=false) {
        $img = 0;
        if ($this->image_uri != NULL){ $img = $this->image_uri; }
        return Img::embed('/myimagesfolder/'.$img, $w, $h, 'default-empty-image.png',
        array("title"=>$this->name));
    }

*Note: $this->image_uri it's attribute image in model

7) in view file: just call the function and your costume sizes

echo $model->img_preview(75,110);

Live example result (HTML):

<img src="http://ae.cineklik.com/images/tb/75x110//movie_photos/finding_nemo_poster.jpg.jpg" width="75px" height="110px" title="Finding Nemo" alt="Finding Nemo">

Examples:

Total 6 comments

#14915 report it
jpablo at 2013/09/20 09:48am
How to use watermark?

You mentioned watermark support, but there not any example nor code related to this function. How can I put a watermark in a thumbnail?

#10141 report it
Anas AbuDayah at 2012/10/07 04:23am
inProcess
  1. function img_preview should be generate html code and url.
  2. when user open this image will send request to url.
  3. when user send this request in preview folder index.php will create new image in preview folder / your folder name / image size / image name if is not exist

I have update the article please read it again

#10140 report it
inProcess at 2012/10/07 03:08am
settings.php

When does the settings.php come into play? I debugged it and from

echo $model->img_preview(75,110);

it goes to my model function img_preview and then to Img functions but it never touched settings.php or index.php. Can you explain the process?

#10138 report it
Anas AbuDayah at 2012/10/07 12:41am
inProcess

image_uri most be a attribute name in model check this in model or column name in database..

and don't forget to and add your custom sizes in settings.php in arrey

#10117 report it
inProcess at 2012/10/04 09:23pm
Got part of it

called it exactly like you mentioned:

echo $model->img_preview(75,110);

and

$this->image_uri

is the name of the image file

but now how do I get the /myimagesfolder/ to point correctly???

#10114 report it
inProcess at 2012/10/04 08:18pm
How to put in my src

6) in model:

public function img_preview($w=false, $h=false) {
        $img = 0;
        if ($this->image_uri != NULL){ $img = $this->image_uri; }
        return Img::embed('/myimagesfolder/'.$img, $w, $h, 'default-empty-image.png',
        array("title"=>$this->name));
    }

7) in view file: just call the function and your costume sizes

echo $model->img_preview(75,110);

where do I pass in my src of the image? what does $this->image_uri mean?

Leave a comment

Please to leave your comment.

Write new article
  • Written by: Anas AbuDayah
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +3
  • Viewed: 8,925 times
  • Created on: Aug 19, 2012
  • Last updated: Apr 15, 2013
  • Tags: images, views