S
S
stkevich2018-04-10 12:57:24
PHP
stkevich, 2018-04-10 12:57:24

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?

Option one
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;
        }
    }
}

Option two
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;
    }
}


My vision:
Option one...
  1. Everything is very clear.
  2. Making typical changes will require changing the structure of methods - just adding a new case to the save and to the load.
  3. Adding non-typical changes will not require significantly more changes than making typical changes - it's still just adding a new case.
Option two...
  1. Significantly less explicit than in the first variant.
  2. Making generic changes will only require making changes to the constant.
  3. Adding
    non-standard changes will require either rewriting the entire code of the save/load methods, or adding crutches.

But I can't decide which option to use... =)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2018-04-10
@Sanasol

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 question

Ask a Question

731 491 924 answers to any question