Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Bonjour,
J'utilise la classe ci-dessous pour redimensionner proportionnellement et dynamiquement des images:
<?php class Zend_Image { const TYPE_JPG = 'jpg'; const TYPE_GIF = 'gif'; const TYPE_PNG = 'png'; public function filenameToMime ($filename) { $types = array( 'jpg' => self::TYPE_JPG, 'gif' => self::TYPE_GIF, 'png' => self::TYPE_PNG ); foreach ($types as $extension => $mime) { if (preg_match('/.'.$extension.'$/', $filename)) { return $mime; } } throw new Zend_Exception('Unknown image type.'); } /** * Returns a PHP image resource */ public function readImage ($filename, $type = NULL) { if ($type === NULL) { $type = $this->filenameToMime($filename); } switch ($type) { case self::TYPE_JPG: return imagecreatefromjpeg($filename); break; case self::TYPE_GIF: return imagecreatefromgif($filename); break; case self::TYPE_PNG: return imagecreatefrompng($filename); break; default: break; } } /** * Doesn't return anything */ public function writeImage ($image, $type = NULL, $filename = NULL) { if ($type === NULL) { $type = $this->filenameToMime($filename); } switch ($type) { case self::TYPE_JPG: imagejpeg($image, $filename); break; case self::TYPE_GIF: imagegif($image, $filename); break; case self::TYPE_PNG: imagepng($image, $filename); break; default: break; } } public function resize ($image, $width = NULL, $height = NULL, $fit = NULL) { $fit = (bool) $fit; // if no bounding box supplied, then return the original image if ($width === NULL && $height === NULL) { return $image; } $origX = $newX = imagesx($image); $origY = $newY = imagesy($image); $origR = $origY / $origX; // if height only was specified if ($width === NULL && $height !== NULL) { if ($origY >= $height || $fit == true) { $newY = $height; $newX = $newY / $origY * $origX; } } // if width only was specified if ($width !== NULL && $height === NULL) { if ($origX >= $width || $fit == true) { $newX = $width; $newY = $newX / $origX * $origY; } } // if width and height specified if ($width !== NULL && $height !== NULL) { // if the image fits and $fit is off, just return the original // image (since it fits in the bounding box) if ($origX < $width && $orimagecopyresampledigY < $height && $fit == false) { return $image; } $newR = $height / $width; // we now need to work out whether to restrain by width or by height if ($origR > $newR) { $newY = $height; $newX = $newY / $origY * $origX; } else { $newX = $width; $newY = $newX / $origX * $origY; } } // now we know the new image's dimensions $new = imagecreatetruecolor($newX, $newY); imagecopyresampled($new, $image, 0, 0, 0, 0, $newX, $newY, $origX, $origY); return $new; } } ?>
J'ai crée une action dans mon contrôleur:
function resizeAction () { require_once 'Zend/Image.php'; $this->_helper->layout->disableLayout(); $this->_helper->removeHelper('viewRenderer'); $this->_helper->viewRenderer->setNoRender(true); $image = new Zend_Image(); $fileName=$_GET['filename']; $monFichier= substr(strrchr($_GET['filename'],'/'),1) ; $im = $image->readImage($_GET['filename']); $new = $image->resize($im, $_GET['width'], $_GET['height'], true); $type = $Image->filenameToMime($filename); $this->getResponse()->setHeader('Content-type',$type) ->setheader('Content-Disposition','inline; filename='.$filename.';'); $this->getResponse()->setBody($image->writeImage($new)); }
Et voilà comment j'appelle mon action:
$img_logo ='<img src=" http://'.$_SERVER['HTTP_HOST'].$this->baseUrl().'/index/resize?filename='. PUBLIC_PATH . 'images/logos/'. $clients['cli_id'] . '.' . $clients['cli_logo'].'&width=50&height=50" alt="'. $clients['client_rs'].'" />';
Mais malheureusement ça me renvoi juste le type mime de fichier et rien d'autres...et je n'ai trouvé rien d'autres pour redimensionner proportionnellement mes images.
Merci d'avance pour votre aide.
Dernière modification par Hall (19-10-2010 16:46:28)
Hors ligne
la library Thumb peut t'aider? Elle est expliquée dans la partie tutoriel
Hors ligne
Merci!
J'ai regardé la méthode (http://www.z-f.fr/forum/viewtopic.php?id=5381) pour redimensionner mes images proportionnellement et dynamiquement.
Je ne passe pas par une aide de vue mais par un contrôleur.
J'ai téléchargé la librairie PhpThumb et l'ai copié sous mon répertoire library puis j'ai crée l'action suivante dans mon contrôleur:
public function thumbAction() { require_once APPLICATION_PATH . '/../library/PhpThumb/ThumbLib.inc.php'; $imagePath=$_GET['imagePath']; $width=$_GET['width']; $height=$_GET['height']; $destPath=$imagePath; $urlPath=$imagePath; $thumb = PhpThumbFactory::create($imagePath); $thumb->resize($width, $height); $file = basename($thumb->getFileName(), '.'.strtolower($thumb->getFormat())).'_thumb.'.strtolower($thumb->getFormat()); $destPath = rtrim($destPath, '/') . '/' . $file; if (!file_exists($destPath)) { $thumb->save($destPath); } $urlPath = rtrim($urlPath) . '/' . $file; return $urlPath; }
Et dans mon script de vue, j'ai:
$img_logo ='<img src=" http://'.$_SERVER['HTTP_HOST'].$this->baseUrl().'/index/thumb?imagePath='. PUBLIC_PATH . 'images/logos/'. $clients['cli_id'] . '.' . $clients['cli_logo'].'&width=50&height=50" alt="'. $clients['client_rs'].'" />';
Seulement à l'exécution, j'ai ce message d'erreur:
Message: File not writeable: http://127.0.0.1/monSite.com/trunk/Sources/public/images/logos/9_thumb.jpg
Est-ce que vous savez d'où ça peut venir?
Dernière modification par Hall (18-10-2010 17:58:17)
Hors ligne