Yii Framework Forum: Simple Image Resize On The Fly - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Simple Image Resize On The Fly

#1 User is offline   __gir 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 21-August 12

Posted 16 August 2013 - 02:49 PM

I use this controller on my webapp to load and resize images (if needed). The controller is based on this code shiftingpixel.com/2008/03/03/smart-image-resizer/

Example of usage: <img src="mysite.com/index.php?r=image/get&image=/images/cover.jpg&width=2000&height=600" />

Two warnings:
* The image folder must be writable by the web server
* It does not work when the query is in path format

Attached File  ImageController.php (11.56K)
Number of downloads: 23

#2 User is offline   le_top 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 460
  • Joined: 08-June 10
  • Location:France (Ile-de-France/Val d'Oise)

Posted 23 October 2013 - 03:48 PM

Interesting Controller. I made some modifications to set the cache directory to the assets directory and I modified the 'crop' parameter to 'false' by default.

I also extend CHtml in my project and I modified 'image' like this:
class YHtml5 {

	 * Generates an image tag.
	 * @param string $src the image URL
	 * @param string $alt the alternative text display
	 * @param array $htmlOptions additional HTML attributes (see {@link tag}).
	 *                           Add height and/or width to enable automatic image resizing.
	 * @return string the generated image tag
	public static function image($src,$alt='',$htmlOptions=array())
		if(!strncmp($src,'/',1)) {
		    if(isset($htmlOptions['height'])) {
		    if(isset($htmlOptions['width'])) {
		    if(count($params)!=1) {
		return self::tag('img',$htmlOptions);

Use is then like this :
echo YHtml5::image('/images/app-logo.jpg',"logo",array('width'=>50)

You may want to create a variant on the 'image' function because specifying both width and height may lead to disproportional scaling.

Attached File(s)


Share this topic:

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users