开发者

Problem wiht wrong file format (gif insted of jpg) when saving images from server - PHP

开发者 https://www.devze.com 2022-12-18 04:16 出处:网络
I am using this peace of PHP code to save and rename some images. foreach ($phones as &$value) { $link_img =

I am using this peace of PHP code to save and rename some images.

foreach ($phones as &$value)
{
    $link_img =
    $handle = @fopen($link_img, "r");

    if ($handle) 
    {
        while (!feof($handle)) 
        {
            $buffer .= fgets($handle, 4096);
        }

        fclose($handle);
        $filename = "files/nokia_".$phone_model.".jpg";
        $mystring = fopen($filename, "wb");开发者_开发知识库
        $handle = fopen($filename, "wb");
        $numbytes = fwrite($handle, $buffer);
        fclose($handle);
        unset($buffer);
    }
}

most of images are JPGs, i think that all files have extension .jpg, but one image I bump in to, have .jpg extension, but its gif (i think), ...

and then my foreach stops :(

how to handle that ?

thanks for your help, I learned much here at stackoverflow, and this is my first question.


All you need to do is update the following line:

$filename = "files/nokia_".$phone_model.".jpg";

To:

$filename = 'files/nokia_' . $phone_model . str_replace('jpeg', 'jpg', image_type_to_extension(exif_imagetype($link_img)));

I don't think this is the reason why your foreach loop is stopping though.


Use the fileinfo functions instead of guessing what type the images are.

0

精彩评论

暂无评论...
验证码 换一张
取 消