• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

php image on center

Hi i want to make a image with size 600 width and 400 height so i want if image for example 200 width and 300 height so it resize and put it no 600 to 400 but on left and right side put black color.
0
umaxim
Asked:
umaxim
  • 2
1 Solution
 
m4trixCommented:
Ok, here's some code that will do that:
 
<?php
// Create a blank 600x400 black image
$dst = imagecreatetruecolor(600, 400);

// Create an image from the image we want in the center of the new image
$src = imagecreatefromjpeg("GoogleWifi.jpg");

// Copy the source image over the destination image (black)
imagecopyresized ($dst,
                  $src,
                  intval((600-imagesx($src))/2),
                  intval((400-imagesy($src))/2),
                  0,
                  0,
                  imagesx($src),
                  imagesy($src),
                  imagesx($src),
                  imagesy($src));


// Set the content type header - in this case image/png
header('Content-Type: image/png');

// Output the image (as a png)
imagepng($dst);

// Free up memory
imagedestroy($dst);
imagedestroy($src);
?>

Open in new window


And using this image:
http://www.skuggen.com/wp-content/uploads/2010/10/GoogleWifi.jpg

as the image you want in the middle (GoogleWifi.jpg), it produces this:
 produced image
I will produce a more robust version that will work with images larger than 600x400 as well (resize them smaller)
0
 
m4trixCommented:
Here's the final solution. Code:
 
<?php
// Create a blank 600x400 black image
$finalx = 600;
$finaly = 400;

$dst = imagecreatetruecolor($finalx, $finaly);

// Create an image from the image we want in the center of the new image
//$src = imagecreatefromjpeg("300x200.jpg"); // narrower and shorter
//$src = imagecreatefromjpeg("300x800.jpg"); //narrower and taller
//$src = imagecreatefromjpeg("750x200.jpg"); //wider and shorter
$src = imagecreatefromjpeg("800x600.jpg"); //wider and taller

$scalex = $finalx/imagesx($src);
$scaley = $finaly/imagesy($src);

if ($scalex < 1 || $scaley < 1) {
	$dstx = imagesx($src) * min($scalex,$scaley);
	$dsty = imagesy($src) * min($scalex,$scaley);
} else {
	$dstx = imagesx($src);
	$dsty = imagesy($src);
}

// Copy the source image over the destination image (black)
imagecopyresized ($dst,
                  $src,
                  intval(($finalx-$dstx)/2),
                  intval(($finaly-$dsty)/2),
                  0,
                  0,
                  $dstx,
                  $dsty,
                  imagesx($src),
                  imagesy($src));


// Set the content type header - in this case image/png
header('Content-Type: image/jpeg');

// Output the image (as a jpeg)
imagejpeg($dst);

// Free up memory
imagedestroy($dst);
imagedestroy($src);
?>

Open in new window


Sample images of various sizes, and the output images associated with them:

images.zip
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now