Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Save Image After imagecopyresampled

Tags:

php

image

$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?

like image 816
JBL Avatar asked Feb 26 '12 11:02

JBL


2 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()

like image 57
MichD Avatar answered Oct 29 '22 00:10

MichD


add this code

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

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

$link='http://example.com/example.png';
echo (substr ($link,strrpos ($link,".")+1));
like image 37
Pooya Estakhri Avatar answered Oct 28 '22 23:10

Pooya Estakhri