lautr.com

Hannes Blog for Development and Stuff besides

zf

simple image view helper for zend framework

| 2 Kommentare

This Helper I use to store and resize Images (external or not) – it uses GD so make sure to have it, of course this is just an example, so you must change the structure of paths/URLs to your needs.


class Zend_View_Helper_Images
{
private $_name;
private $_source;
static private $_webpath;
static private $_fspath;

public function __construct() {
if(null == $this->_fspath){
$this->_fspath = str_replace('application','',APPLICATION_PATH)."public/images/tumbs/";
$this->_webpath = "/images/thumbs/";
}
}

public function scale($x = false,$y = false)
{

if(!file_exists($this->_fspath.'x'.$x.'y'.$y.$this->_name)){
$infos = getimagesize($this->_source);

switch($infos['mime']) {
case "image/pjpeg":
case "image/jpeg":
case "image/jpg":
$image=imagecreatefromjpeg($this->_source);
break;
case "image/gif":
$image=imagecreatefromgif($this->_source);
break;
case "image/png":
case "image/x-png":
$image=imagecreatefrompng($this->_source);
break;
}
$newImage = imagecreatetruecolor($x,$y);
imagecopyresampled($newImage,$image,0,0,0,0,$x,$y,$infos[0],$infos[1]);

imagejpeg($newImage,$this->_fspath.'x'.$x.'y'.$y.$this->_name);
imagedestroy($newImage);
}

return $this->_webpath.'x'.$x.'y'.$y.$this->_name;

}

public function images($imageurl)
{
$this->_source = $imageurl;
preg_match('#[^/]+\.[\w]+$#',$imageurl,$match);
$this->_name = $match[0];
return $this;
}
}

in that cas the Implementation looks like:


<img src="<?= $this->images($this->some_image_url)->scale(150,200) ?>" alt=""/>

Autor: Hannes

Hi! I’m Johannes Lauter a 26 year old Web Application Developer based in Berlin ... more

2 Kommentare

  1. sir please give me steps to create this thumbnail images means how to in initialize in controller and in view and in bootstrap loader file.

  2. Hello. I have some problem with this helper… Maybe you can solve that. I try more helper like this, but always have same problem. When I call helper in view always have error “failed to open stream: No such file or directory…” for gd function like getimagesize, and other… I use wamp on windows 7, gd is run… When go directly to img path, that img exist… I don’t know what can be problem…I check permission to folder, and that is OK too… Please help :)

Hinterlasse eine Antwort

Pflichtfelder sind mit * markiert.

*