How to set default size of a PHP image

Asked

Viewed 674 times

3

I wonder if there is any way to define the height, and the default width in php, I saw some forms, but I used an external class, I wonder if there is any way to use the GD library...

$dest = imagecreatefromjpeg('imgs/bg.jpg');
$tamanho = imagesx($dest);
$width = 594;
$height = 387;

$src = imagecreatefrompng('imgs/textura.png');
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge($dest, $src, -30, -50, 0, 0, 700, 500, 50); 
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
  • Here I posted an answer that shows how to size an image using the GD http://answall.com/a/9757/70 library - By comments you can understand what each part of the code does.

  • Still a little complicated, I already have a code ready, I’ll edit the question to improve, this my code it puts an image on top of another, but when the image is less than 500px the one on top does not centralize, the solution would be to resize or use if for different sizes...

  • to center just do the bill: margem esquerda = ( largura da maior - largura da menor ) / 2, and the same thing for the upper margin.

  • I didn’t understand what you said, the margin I set on this line >imagecopymerge($dest, $src, -30, -50, 0, 0, 700, 500, 50); Ali on "-30"

  • What you need to do is replace the -30 and the -50 with the formula I posted. To see how to get these variables, just give a read on that answer I made the link.

  • When you say "largest size" and "Smallest size" is the largest/smallest image tamamho?

Show 1 more comment

1 answer

1


What you want can be solved this way:

$dest = imagecreatefromjpeg('imgs/bg.jpg');
$tamanho = imagesx($dest);
$width = 594;
$height = 387;

$larguraPadrao = 700;
$alturaPadrao  = 500;

$centro_x      = ceil( ( $larguraPadrao-$width ) / 2 );
$centro_y      = ceil( ( $alturaPadrao-$height ) / 2 );

$src = imagecreatefrompng('imgs/textura.png');
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopymerge($dest, $src, $centro_x, $centro_y, 0, 0, $larguraPadrao, $alturaPadrao, 50);
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);

Browser other questions tagged

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