Bene, ho finito appena adesso di scrivere la mia prima classe PHP relativa al resize delle immagini. Ho subito pensato di proporvela per averne una considerazione. Ho cercato di attenermi al principio del "Si sa cosa fà, ma non come lo fà" e spero di esserci riuscito bene... La classe a voi esperti risulterà basilare, ma per me si tratta di un grande salto in avanti, poichè nonostante io usi php da diverso tempo ormai, non mi ero mai cimentato con l'OOP. Premesso ciò, nel caso in cui abbiate suggerimenti, vi prego di esporli per aiutarmi a migliorare le mie competenze nell'OOP. Dopo aver rotto le scatole, vi mostro la classe:
Codice PHP:
# +-----------------------------------------------------------------------+
# | class.imageresize.php
# +-----------------------------------------------------------------------+
# | Create by Antonio Ciccia
# | Version 0.1
# | Last modified 24/10/2008
# | Email [email]antonio.ufficiale@gmail.com[/email]
# +------------------------------------------------------------------------+
# | This program is free software; you can redistribute it and/or modify
# | it under the terms of the:
# | Creative Commons Attribuzione-Non commerciale 2.5 Italia License
# +-----------------------------------------------------------------------+
# | Please give credit on sites that use class.imageresize
# | and submit changes of the script so other people can use them as well.
# +-----------------------------------------------------------------------+
Class ImageResize {
private $image;
private $extension;
private $headertype;
private $width;
private $height;
private $newwidth;
private $newheight;
private $gd2type;
private $resizedimage;
public function ResizeThisImage($img, $size) {
$this->GettingImage($img);
$this->GettingImageExtension();
$this->GettingImageHeaderType();
$this->GettingImageNewSize($size);
$this->GettingImageGD2Type();
$this->GettingImageResized();
}
private function GettingImage($img) {
#Getting image
$this->image=$img;
}
private function GettingImageExtension() {
#Getting image extension
$ext=explode(".", $this->image);
$lastdot=count($ext)-1;
$lastdot=$lastdot;
$this->extension=strtolower($ext[$lastdot]);
#Changing jpeg in jpg
if ($this->extension=="jpeg") $this->extension="jpg";
}
private function GettingImageHeaderType() {
#Switching image type
switch($this->extension) {
#Setting header type
case "gif": $this->headertype=header('Content-Type: image/gif'); break;
case "jpg": $this->headertype=header('Content-type: image/jpeg'); break;
case "png": $this->headertype=header('Content-Type: image/gif'); break;
}
}
private function GettingImageNewSize($size) {
#Setting new image size
list($this->width, $this->height)=getimagesize($this->image);
$this->newwidth=$this->width*$size;
$this->newheight=$this->height*$size;
$this->resizedimage=imagecreatetruecolor($this->newwidth, $this->newheight);
}
private function GettingImageGD2Type() {
#Switching image type
switch($this->extension) {
#Setting GD2 create from type
case "gif": $this->gd2type=imagecreatefromgif($this->image); break;
case "jpg": $this->gd2type=imagecreatefromjpeg($this->image); break;
case "png": $this->gd2type=imagecreatefrompng($this->image); break;
}
}
private function GettingImageResized() {
#Resizing image
imagecopyresized($this->resizedimage, $this->gd2type, 0, 0, 0, 0, $this->newwidth, $this->newheight, $this->width, $this->height);
#Switching image type
switch($this->extension) {
#Creating resized image
case "gif": imagegif($this->resizedimage); break;
case "jpg": imagejpeg($this->resizedimage); break;
case "png": imagepng($this->resizedimage); break;
}
}
}
#Creo un'istanza della classe ImageResize
$image = new ImageResize();
#Richiamo la funzione che si occupa di gestire il resize passando ad essa il percorso relativo all'immagine e la nuova dimensione
$image->ResizeThisImage($image, $newsize);
?>