I've created my first class ever so any code critique is welcome 🙂
Please note that form validating is not included in this class, that function belongs to a different class...
<?php
class Image {
var $img_width, $img_height, $img_type, $img_temp, $img_name;
var $ThumbDir, $ThumbWidth, $ThumbHeight, $Final_ThumbWidth, $Final_ThumbHeight, $PhotoDir, $PhotoWidth, $PhotoHeight, $Final_PhotoWidth, $Final_PhotoHeight; #Setup variables
function Image () {
if (!$this->ThumbDir) {
$this->ThumbDir = "Thumbs/";
}
if (!$this->ThumbWidth) {
$this->ThumbWidth = "150";
}
if (!$this->ThumbHeight ) {
$this->ThumbHeight = "150";
}
if (!$this->PhotoDir) {
$this->PhotoDir = "Photos/";
}
if (!$this->PhotoWidth) {
$this->PhotoWidth = "800";
}
if (!$this->PhotoHeight ) {
$this->PhotoHeight = "600";
}
}
function Setup ($ThumbDir, $ThumbWidth, $ThumbHeight, $PhotoDir, $PhotoWidth = "", $PhotoHeight = "") {
$this->ThumbDir = $ThumbDir;
$this->ThumbWidth = $ThumbWidth;
$this->ThumbHeight = $ThumbHeight;
$this->PhotoDir = $PhotoDir;
$this->PhotoWidth = $PhotoWidth;
$this->PhotoHeight = $PhotoHeight;
}
#Actual Imageprocessing functions
function ImgProp ($img_temp, $name) {
$this->img_temp = $img_temp;
$img_prop = getimagesize($this->img_temp);
$this->img_width = $img_prop[0];
$this->img_height = $img_prop[1];
$this->img_type = $img_prop['mime'];
$this->img_name = $name;
}
function UniqName () {
$this->img_name = uniqid(substr($this->img_name, 0, 4), true).strrchr($this->img_name,".");
}
function CreateThumb () {
if ($this->img_width > $this->ThumbWidth) {
$xyrelation = $this->img_width / $this->img_height;
$this->Final_ThumbWidth = round($this->ThumbWidth * $xyrelation);
if ($this->Final_ThumbWidth > $this->ThumbWidth) {
echo $this->Final_ThumbWidth = $this->ThumbWidth;
echo $this->Final_ThumbHeight = round($this->Final_ThumbWidth / $xyrelation);
}
}
elseif ($this->img_height > $this->ThumbHeight) {
$xyrelation = $this->img_height / $this->img_width;
$this->Final_ThumbHeight = round($this->ThumbHeight * $xyrelation);
if ($this->Final_ThumbHeight > $this->ThumbHeight) {
echo $this->Final_ThumbHeight = $this->ThumbHeight;
echo $this->Final_ThumbWidth = round($this->Final_ThumbHeight / $xyrelation);
}
}
else {
$this->Final_ThumbWidth = $this->img_width;
$this->Final_ThumbHeight = $this->img_height;
}
$thumb = imagecreatetruecolor($this->Final_ThumbWidth, $this->Final_ThumbHeight); //maak een lege afbeelding aan met de juiste afmetingen.
switch ($this->img_type) { // Kijk wat voor extensie de afbeelding heeft.
case "image/pjpeg":
imagecopyresampled($thumb, imagecreatefromjpeg($this->img_temp), 0,0,0,0, $this->Final_ThumbWidth, $this->Final_ThumbHeight, $this->img_width, $this->img_height);
imagejpeg($thumb, $this->ThumbDir.$this->img_name, 85); // sla de nieuwe afbeelding op
break;
case "image/jpeg":
imagecopyresampled($thumb, imagecreatefromjpeg($this->img_temp), 0,0,0,0, $this->Final_ThumbWidth, $this->Final_ThumbHeight, $this->img_width, $this->img_height);
imagejpeg($thumb, $this->ThumbDir.$this->img_name, 85); // sla de nieuwe afbeelding op
break;
case "image/png":
imagecopyresampled($thumb, imagecreatefrompng($this->img_temp), 0,0,0,0, $this->Final_ThumbWidth, $this->Final_ThumbHeight, $this->img_width, $this->img_height);
imagepng($thumb, $this->ThumbDir.$this->img_name, 85); // sla de nieuwe afbeelding op
break;
}
imagedestroy($thumb);
}
function ResizePhoto () {
if ($this->img_width > $this->PhotoWidth || $this->img_height > $this->PhotoHeight) {
$xyrelation = $this->img_width / $this->img_height;
$this->Final_PhotoWidth = round($this->PhotoWidth * $xyrelation);
if ($this->Final_PhotoWidth > $this->PhotoWidth) {
$this->Final_PhotoWidth = $this->PhotoWidth;
$this->Final_PhotoHeight = round($this->Final_PhotoWidth / $xyrelation);
}
}
elseif ($this->img_height > $this->PhotoHeight) {
$xyrelation = $this->img_height / $this->img_width;
$this->Final_PhotoHeight = round($this->PhotoHeight * $xyrelation);
if ($this->Final_PhotoHeight > $this->PhotoHeight) {
echo $this->Final_PhotoHeight = $this->PhotoHeight;
echo $this->Final_PhotoWidth = round($this->Final_PhotoHeight / $xyrelation);
}
}
else {
$this->Final_PhotoWidth = $this->img_width;
$this->Final_PhotoHeight = $this->img_height;
}
}
function NewImage () {
$picture = imagecreatetruecolor($this->Final_PhotoWidth, $this->Final_PhotoHeight); //maak een lege afbeelding aan met de juiste afmetingen.
imagecopyresampled($picture, imagecreatefromjpeg($this->img_temp), 0,0,0,0, $this->Final_PhotoWidth, $this->Final_PhotoHeight, $this->img_width, $this->img_height);
imagejpeg($picture, $this->PhotoDir.$this->img_name, 75); // sla de nieuwe afbeelding op
imagedestroy($picture);
}
function NewMsgImage($message) {
if ($this->Final_PhotoWidth < 200) {
$this->NewImage();
}
else {
$picture = imagecreatetruecolor($this->Final_PhotoWidth, $this->Final_PhotoHeight); //maak een lege afbeelding aan met de juiste afmetingen.
imagealphablending($picture, TRUE); // Geeft aan dat de afbeelding ook met alphalayers om kan gaan
$black = imagecolorallocate($picture, 0, 0, 0); // Kleuren definieren
$white = imagecolorallocate($picture, 255, 255, 255);
$alphablack = imagecolorallocatealpha($picture, 0, 0, 0,100);
imagecopyresampled($picture, imagecreatefromjpeg($this->img_temp), 0,0,0,0, $this->Final_PhotoWidth, $this->Final_PhotoHeight, $this->img_width, $this->img_height);
imagefilledrectangle($picture, 0, 0, 200, 30, $alphablack); //linksboven een gevulde rechthoek van 200*60 maken
imagestring($picture, 3, 6, 6, "Copyright $message", $white); // copyright weergeven
imagejpeg($picture, $this->PhotoDir.$this->img_name, 100); // sla de nieuwe afbeelding op
imagedestroy($picture);
}
}
function Upload () {
if (is_uploaded_file($this->img_temp)) {
move_uploaded_file ($this->img_temp, $this->PhotoDir.$this->img_name);
}
}
}
?>
Thx 🙂