euploadedimage a simple class to easily upload and resize images

  1. Requirements
  2. Usage
  3. Resizing
  4. The thumb Attribute
  5. Changelog

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

11 2
21 followers
4 525 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Others
Developed by: nickcv
Created on: Apr 11, 2011
Last updated: 6 years ago

Downloads

show all

Related Extensions