-1
The code below theoretically would be to reduce the width and height of the images [JPG FORMAT ONLY] received by CURL and store on the server, but the image created is only an image in the 800x600 measures with black background, what I intended to do was only resize the images so the server would not store the original size images taken from the remote server due to the size which are large images which for the purpose have no need and would also save disk space.
$urlImg = 'https://images.site.com/image.jpg';
$url = $urlImg;
$imgName = str_replace(":","",$url);
$imgName = str_replace("//","",$imgName);
$imgName = str_replace("/","",$imgName);
$imgName = str_replace("_","",$imgName);
$path = './upload/' . $imgName;
$curl = curl_init();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt_array($curl, [
CURLOPT_URL => $url,
// CURLOPT_BINARYTRANSFER => 1, --- No effect from PHP 5.1.3
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FILE => $file,
CURLOPT_TIMEOUT => 50,
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
]);
$data = curl_exec($ch);
curl_close($ch);
$im = imagecreatefromstring($data);
$x = 800;
$y = 600;
$im2 = imagecreatetruecolor($x,$y);
imagecopyresized($im2,$im,0,0,0,0,$x,$y,imagesx($im),imagesy($im));
imagecopyresampled($im2,$im,0,0,0,0,$x,$y,imagesx($im),imagesy($im));
$returnCreateImage = imagejpeg($im2, $path);
if ( $returnCreateImage ) {
imagedestroy($im);
imagedestroy($im2);
echo "[Get]\n";
}
The code worked the way you wanted before the modification to reduce the images, which is this below.
$urlImg = 'https://images.site.com/image.jpg';
$url = $urlImg;
$imgName = str_replace(":","",$url);
$imgName = str_replace("//","",$imgName);
$imgName = str_replace("/","",$imgName);
$imgName = str_replace("_","",$imgName);
$path = './upload/' . $imgName;
$curl = curl_init();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Update as of PHP 5.4 array() can be written []
curl_setopt_array($curl, [
CURLOPT_URL => $url,
// CURLOPT_BINARYTRANSFER => 1, --- No effect from PHP 5.1.3
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FILE => $file,
CURLOPT_TIMEOUT => 50,
CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
]);
$data = curl_exec($ch);
curl_close($ch);
if(file_put_contents($path, $data)) {
echo "[Get]\n";
}
Dear Eliseu, let me ask you a question, do you want to convert the image to JPEG and put the background that would be previously transparent to white or do you want to keep the transparence in fact? It is that this was not evident/clear in your question, ps: the downvote is not mine, but the reason perhaps was this lack of detail of what it really needs. I stand by ;)
– Guilherme Nascimento
Hi, I edited the question, the only intention is to resize the original received by CURL and store because it is unnecessary the original size of the remote server for the purpose and will also save disk space. = D
– ElvisP
Let me get this straight, you want to resize any type of image of any format and just save to your server, would that be?
– Guilherme Nascimento