Remote image received by CURL and reduced in PHP

Asked

Viewed 132 times

-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 ;)

  • 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

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

1 answer

0

I believe this is because you are saving JPEG and not PNG. In theory JPEG does not support transparency, I say "in thesis" because it seems to support, but it is not so common to use such a resource.

Also, you are using the imagecreatetruecolor which creates a black image by default.


I don’t have PHP to test, I’m just relying on the documentation.

$original = imagecreatefromstring($data);

// Os tamanhos das imagens:
[$cx, $cy] = [800, 600];
[$ox, $oy] = [imagesx($original), imagesy($original)];

// Cria a imagem:
$comprimido = imagecreatetruecolor($cx,$cy);

// Define a imagem para transparente:
imagefill($comprimido, 0, 0, imagecolorallocatealpha($comprimido, 0, 0, 0, 127));

// Habilita a transparência:
imagealphablending($comprimido, false);
imagesavealpha($comprimido, true);

imagecopyresized($comprimido, $original, 0, 0, 0, 0, $cx, $cy, $ox, $oy);
imagecopyresampled($comprimido, $original, 0, 0, 0, 0, $cx, $cy, $ox, $oy);

// Salva como PNG (não JPEG):
$returnCreateImage = imagepng($comprimido, $path);

This should be enough, but I can’t test it. I believe that even if it doesn’t work, the problem is related to PNG.

  • 1

    Just as a note for details about JPEG and transparency: The transparence in JPEG refers to JPEG 2000 and the PHP functions seem to only support JPEG200 for "size", see: as stated in https://www.php.net/manualen/intro.image.php, I did not find mention about jpeg2000 in the other functions of GD, which probably refers to getimagesize and not GD, so even though jpeg2000 supports transparence, it is not the same thing as jpeg and the test I made the imagejpeg does not support saving with alpha, getting a black background in its place.

  • 1

    Note ²: Maybe Imagick (name used in PHP that refers to ImageMagick) has support for JPEG2000, but is also not well documented, of course both GD and Imagemagick are part of other things and not internal PHP, so maybe on the official Imagemagick website you should detail the support (which exists) just have to check if the version used in PHP is equivalent to that it has support and if the php functions have support with the parameters, but remember that to install it only via pecl, you can even try to install manually by yourself, which is probably quite laborious.

  • hi, I left to wish in the explanation, it is only jpg format same, and resize the size of the original to store only.

  • @Eliseub. The problem is precisely the original image, it is a PNG, with transparency, at least in its example. You said there was a black background, so I deduced that the image was not keeping the background transparent. If the original image is PNG, it would be preferable if the resized image were also.

  • Got it, the img is not PNG anyway, it’s just JPG, it was unfortunate to take a web image to put in the example kkkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.