PYovchevski
PYovchevski

Reputation: 115

Image resize with php image magician

I'm using PHP IMAGE MAGICIAN for resizing the images of my website.

When I try to resize the image with a changed extension (e.g. changing image.jpg to image.gif), the function returns an error because the image is invalid in this situation. I want to avoid this error.

I tried a lot of methods to check is the image valid but without success.

The error which appear is:

"file /Users/.../uploads/1541963916_4dd672e5f3a3060ced41f3f7975453c9.gif is missing or invalid"

Every image I upload to my website I am renaming to a new filename with its extension.

This is the part of code which I am using.

$workWithImage = new imageLib(UPLOADS_DIR . $original);
$workWithImage->resizeImage($width, $height, $type);
$workWithImage->saveImage(UPLOADS_DIR . $thumbnail, $imageQuality);

I searched for this problem but I could not find a solution.

Upvotes: 0

Views: 545

Answers (1)

Valdars
Valdars

Reputation: 863

Unfortunately looking at source this library seems to not handle this situation at all. You can create bug report at library home page. What you can do is compare mime type of file with it's extension and either do you own error handling or rename file name to have correct extension. Mime type detection code used by this library is

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($finfo, $file);
finfo_close($finfo);
switch($mimeType) {
    case 'image/jpeg':
    case 'image/gif':
    case 'image/png':
    case 'image/bmp':
    case 'image/x-windows-bmp':
        $isImage = true;
        break;
    default:
        $isImage = false;
}

Upvotes: 1

Related Questions