PHP Save Image After imagecopyresampled

$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

How can i save the resized image to folder/ ? And how can I detect the image type is jpg/png/gif?

6 Answers

To save the image to a file, you can use any of these: imagejpeg(), imagepng(), or imagegif(), depending on your desired output format.

To detect the image type, you can just check the file's extension and base yourself on that. However, sometimes people manually change the extension of an image file, thinking that actually changes the image type, so it's always a good idea to check whether imagecreatefrom returned an image resource rather than false.

For a quick way to return just the extension of a file:

$ext = pathinfo($path_to_file, PATHINFO_EXTENSION);

Manual entry on pathinfo()

add this code

imagepng($iOut,'pic/mypic.png',3);

& this code to get your pics format from an external source

$link='
echo (substr ($link,strrpos ($link,".")+1));

You can define any type image:

 // Save the image as 'simpletext.jpg' imagejpeg($im, 'path/to/your/image.jpg'); // or another image imagepng($im, 'path/to/your/image.png');

See examples here

$filename = 'path/to/original/file.xxx'; // where xxx is file type (jpg, gif, or png)
$newfilename = 'path/to/resized/file.xxx'; // where xxx is file type (jpg, gif, or png)
$path_parts = pathinfo($filename);
if ($path_parts['extension'] == 'jpg') { $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_p, $newfilename);
} elseif ($path_parts['extension'] == 'gif') { $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromgif($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagegif($image_p, $newfilename);
} elseif ($path_parts['extension'] == 'png') { $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefrompng($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagepng($image_p, $newfilename);
} else { echo "Source file is not a supported image file type.";
}
1

After you applied imagecopyresampled(), the $dst_image will be your image resource identifier.

Just applying the imagecopyresampled() function does not automagically also save it to the file system.

So you will need to save it, using one of the functions imagejpeg(), imagepng()

// Output
imagejpeg($dst_image, 'new-image.jpg', 100);

to save image as jpg see imagejpeg function

to get image extension use

$path_parts = pathinfo($filename);
echo $path_parts['extension'];

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like