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
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 ).
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
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);
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
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?
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');*/
hello
can you uploaded a demo of this extension how it works.
thanks.
when not upload file
@Charger just give a conditional on __construct()
protected function __construct($name,$tempName,$type,$size,$error) { if( !empty($tempName) ) { //original code } else return; }
not supported image
Hi guys
I try upload image server like example, but this error show. Any suggestion
Greeting
Better solution to no file upload
@saebaryo I found a better way to do this - insert after setting _error, but before loading image info:
protected function __construct($name,$tempName,$type,$size,$error) { $this->_name=$name; $this->_tempName=$tempName; $this->_type=$type; $this->_size=$size; $this->_error=$error; if(empty($tempName)) return; // load image info $image_info = getimagesize($tempName);
This way EUploadedImage behaves almost like CUploadedFile in matter of doing things like
if($uploadedImage=EUploadedImage::getInstance($model,'image'))
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.