简体   繁体   中英

Resize photo with every extensions

i got the resize photo script frm some website and basically it work just fine but the problem is: i can only use it to upload JPG photo only if i try to upload other extensions such as : png,gif i will get the error here is my code:

<?
if(trim($_FILES["fileUpload"]["tmp_name"]) != "")
{
    $images = $_FILES["fileUpload"]["tmp_name"];
    $new_images = "Thumbnails_".$_FILES["fileUpload"]["name"];
    copy($_FILES["fileUpload"]["tmp_name"],"MyResize/".$_FILES["fileUpload"]["name"]);
    $width=100; //*** Fix Width & Heigh (Autu caculate) ***//
    $size=GetimageSize($images);
    $height=100;
    $images_orig = ImageCreateFromJPEG($images);
    $photoX = ImagesX($images_orig);
    $photoY = ImagesY($images_orig);
    $images_fin = ImageCreateTrueColor($width, $height);
    ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
    ImageJPEG($images_fin,"MyResize/".$new_images);
    ImageDestroy($images_orig);
    ImageDestroy($images_fin);  }

?>

Again my Question is: how to make this script works with all extensions ?

Thanks a lot,

Your PHP script most probably uses ImageMagick.

JPG, GIF, PNG are different image formats, with different kind of data stored.

Instructions ImageCreateFromJPEG, ImageCreateTrueColor and ImageJPG apply to JPG format only, so... start to search in ImageMagick's documentation for GIF and PNG.

You may need to get the extension of the file by using pathinfo() and use ImageCreateFromJPEG , ImageCreateFromPNG , ImageCreateFromGIF , ImageJPEG , ImagePNG , and ImageGIF accordingly:

<?
if(trim($_FILES["fileUpload"]["tmp_name"]) != "")
{
    $images = $_FILES["fileUpload"]["tmp_name"];
    $new_images = "Thumbnails_".$_FILES["fileUpload"]["name"];
    copy($_FILES["fileUpload"]["tmp_name"],"MyResize/".$_FILES["fileUpload"]["name"]);
    $width=100; //*** Fix Width & Heigh (Autu caculate) ***//
    $size=GetimageSize($images);
    $height=100;
    $ext = pathinfo($images, PATHINFO_EXTENSION);
    if($ext == "jpg")
    {
       $images_orig = ImageCreateFromJPEG($images);
    }
    elseif($ext == "png")
    {
       $images_orig = ImageCreateFromPNG($images);
    }
    elseif($ext == "gif")
    {
       $images_orig = ImageCreateFromGIF($images);
    }
    $photoX = ImagesX($images_orig);
    $photoY = ImagesY($images_orig);
    $images_fin = ImageCreateTrueColor($width, $height);
    ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
    if($ext == "jpg")
    {
       ImageJPEG($images_fin,"MyResize/".$new_images);
    }
    elseif($ext == "png")
    {
       ImagePNG($images_fin,"MyResize/".$new_images);
    }
    elseif($ext == "gif")
    {
       ImageGIF($images_fin,"MyResize/".$new_images);
    }

    ImageDestroy($images_orig);
    ImageDestroy($images_fin);  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM