Yii 1.1: euploadedimage

a simple class to easily upload and resize images
21 followers

this class was supposed to be an extension of the core CUploadedFile, but unfortunately late static binding is supported just by php 5.3 and the core class should be rewritten to support it.

With this class you can easily upload and resize your images and create thumbnails if you need to.

This extension support alpha channel (transparency) for png files

Requirements

Tested on Yii 1.1.7, but should support any 1.x version
This extension only supports GIF, JPG and PNG images

Usage

There's not a real installation of this extension, you just have to import the class file in someway.
You can do it using the Yii::import command inside your controller, or you can just add the path to the file in the import section of your config file.

It's extremely easy to use, you just have to do the same things you where used to when using CUploadedFile, you just have a couple of more options.

$model->image = EUploadedImage::getInstance($model,'image');
$model->image->maxWidth = 600;
$model->image->maxHeight = 300;
 
$model->image->thumb = array(
    'maxWidth' => 200,
    'maxHeight' => 500,
    'dir' => 'thumbs',
    'prefix' => 'asdf_',
);
 
if ($model->image->saveAs('img/ciao.jpg'))
    echo 'ok!';
else
    echo 'error';
  • maxWidth the image max width. if the image is bigger it will be resized to match it
  • maxHeight the same as maxWidth but for the image height
  • thumb use this attribute if you want to create a thumbnail of your image as well.

Resizing

every single image will be resided without changing the proportions between width and height.
You can give both maxWidth and maxHeight parameter without worrying about your image being distorted.
This extension will first resize the image according to the maxWidth value (just if the current value exceed it, of course). Then, if the resized image height is still higher then the maxHeight allowed the image will be resized again, still keeping the proportions.

The thumb Attribute

  • the maxHeight and maxWidth parameters of thumb serve the same purpose explained above.
  • the dir parameter just states the name of the directory where thumbnails are supposed to go, and is entirely optional.
  • the prefix parameter is the prefix that will be put in front of the image name. This is optional as well and if you won't specify any thumb_ will be used as a prefix.

In the save method you will have just to specify the name/path of the main image, the extension will take care of doing the extra work for the thumbnail.

So as we can see the above example would create a new image inside the img directory named ciao.jpg, and a second image named asdf_ciao.jpg inside a directory named thumbs located inside the img directory.
So this will be the two paths of the two images:
img/ciao.jpg
img/thumbs/asdf_ciao.jpg

Changelog

version 1.2
added bugfix for transparency issues with png files that are not resized.

version 1.1
added alpha channel support for PNG images

Total 7 comments

#12313 report it
rajesh chaurasia at 2013/03/13 09:47am
hello

can you uploaded a demo of this extension how it works. thanks.

#10959 report it
manuel-84 at 2012/12/06 10:38am
issues

@jesse use DIRECTORY_SEPARATOR when specifying the filepath where to save

@nickcv i had to comment-out lines 192/193 to skip file extension verification and check it in a validation form

/*else
            throw new CException('not supported image', '400');*/
#9011 report it
Charger at 2012/07/12 12:33pm
Error when I not upload file

If file field not requiren and not filed. When call EUploadedImage::getInstance got warning "getimagesize() [function.getimagesize]: Filename cannot be empty" in EUploadedImage.php(189) This in bug?

#7751 report it
Lastik at 2012/04/14 10:20pm
if you can`t extract this archive

if you can`t extract this archive

  • first - rename file EUploadedImage-1.2.tgz to EUploadedImage-1.2.gz

  • extract it

  • rename extracted file to EUploadedImage-1.2.tar

  • extract

All done!

2 author: - good extension but for future - create another type of archive

#6327 report it
Dan S.K. at 2011/12/30 01:03am
bug

When you save an unresized png it screws up transparency. To avoid it you have to add

imagealphablending($this->_image, false);
imagesavealpha($this->_image, true);

before return imagepng($this->_image,$file_name);

#4996 report it
Jesse at 2011/09/05 01:47am
Error: Unable to open ( thumbnail error )

Hi, first thank you for this great extension!

Saving the resized files worked perfect however I kept getting this error when using the thumbnail feature.

ERROR: Unable to open 'C/\thumb_:/xampp/htdocs/FoodAndNews/images/test/93 celica 002.jpg' for writing: Invalid argument

I am using this command for the save function.

$image->saveAs( Yii::getPathOfAlias('webroot').'/images/test/'.$image->name )

It seems that the extension doesn't like the full path that I gave it. (maybe I missed something?)

I changed line 247 in the extension "EUploadedImage.php" and it seems to work for me.

OLD:

Line 247: if ($this->saveImage( substr_replace($file, $thumb_dir.$thumb_prefix, strrpos($file, DIRECTORY_SEPARATOR) + 1, 0) ))

NEW:

Line 247: if ($this->saveImage( dirname( $file ). $thumb_dir . $thumb_prefix . basename( $file ) ) )

Jesse

#4764 report it
Dan S.K. at 2011/08/12 01:53pm
doesn't check existence of files

Hi there. getInstance methods are supposed to return NULL if no file was uploaded. Your extension doesn't check if it exists, so there is an error during the getimagesize() call. Just wanted to let you know ).

Leave a comment

Please to leave your comment.

Create extension