converting DURING upload before saving on server png/gif to jpg

后端 未结 2 527
礼貌的吻别
礼貌的吻别 2021-01-15 06:29

So I have an image upload script. It uploads the image and saves it to the space on the server. What I can\'t seem to get my head around is say, when the user uploads a .png

2条回答
  •  旧时难觅i
    2021-01-15 06:55

    Using GD, and assuming $images is the directory where you store your images (with ending slash), and $name - the file name of the original image:

    $destinationPath = $images . basename($name, $ext) . '.jpg';
    $source = imagecreatefrompng($images . $name);
    imagejpeg($source, $destinationPath, 75);
    imagedestroy($source);
    

    Or with Imagick:

    $image = new Imagick($images . $name);
    $image->writeImage($destinationPath);
    $image->destroy();
    

提交回复
热议问题