Answer the question
In order to leave comments, you need to log in
What is the best way to arrange the function of loading/saving an image?
Actually, the essence of the question is simple, there are two options for loading / saving an image, which one is preferable? Or are both options shit?
class test {
protected function loadImage()
{
$this->imageInfo = getimagesize($this->image->tempName);
switch ($this->imageInfo[2]) {
case IMAGETYPE_GIF :
$this->imageSrc = imagecreatefromgif($this->image->tempName);
break;
case IMAGETYPE_JPEG :
$this->imageSrc = imagecreatefromjpeg($this->image->tempName);
break;
case IMAGETYPE_PNG :
$this->imageSrc = imagecreatefrompng($this->image->tempName);
break;
default :
throw new \Exception("Can't load image");
break;
}
}
protected function saveImage($imageFile, $imageName, $imageType)
{
switch ($imageType) {
case IMAGETYPE_GIF :
imagegif($imageFile, $imageName);
break;
case IMAGETYPE_JPEG :
imagejpeg($imageFile, $imageName);
break;
case IMAGETYPE_PNG :
imagepng($imageFile, $imageName);
break;
}
}
}
class test {
CONST IMAGE_MIME_TYPE_ID = [
1 => "gif",
2 => "jpeg",
3 => "png",
];
protected function loadImage()
{
$this->imageInfo = getimagesize($this->image->tempName);
$imageMime = static::IMAGE_MIME_TYPE_ID[$this->imageInfo[2]];
if ($imageMime) {
$func = "imagecreatefrom$imageMime";
$this->imageSrc = $func($this->image->tempName);
} else {
throw new \Exception("Can't load image");
}
}
protected function saveImage($imageFile, $imageName, $imageType)
{
$func = 'image' . static::IMAGE_MIME_TYPE_ID[$imageType];
$func($imageFile, $imageFileName);
return $imageName;
}
}
Answer the question
In order to leave comments, you need to log in
uhm, what is the sacred meaning of uploading images to imagecreate and saving them right there?
Why not just check mime+file extension+size and move to destination folder with name change if needed?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question