Resize in image with PHP

Asked

Viewed 173 times

0

I need to resize all images when sharing on Facebook. There are several images of various sizes in the product listing, but when I share the link on Facebook, depending on the size of the image, Facebook put in the larger picture box or smaller box, depending on the size. What I need to do, is to play to the meta OG the image with the size changed.

I found a function that gives resize, but it gives in percentage and I need the image to be readjusted to the exact size of 600x315px.

Follows the code:

<?php
// Imagem e novo tamanho (em porcentagem)
$filename = '1.jpg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($thumb);

?>
<meta property="og:image" content=""/>

After this, I need to play the image readjusted in the meta og.

1 answer

3


Instead of using:

$newwidth = $width * $percent;
$newheight = $height * $percent;

Change to:

$newwidth = 600;
$newheight = 315;

But the images will be stretched.

To put on your facebook meta:

<meta property="og:image" content="http://www.urldoseusite.com.br/pasta/onde/ficam/suas/imagens/<?php echo $filename; ?>"/>

Browser other questions tagged

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