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

Standardizing random image sizes by padding with PHP

Standardizing random image sizes by padding with PHP

ImageMagick or GD, doesn't matter, as long as it's in PHP.

I am extracting several images from a website.  I need every image to be padded to fit 280x210.  Padding color should be #CCCCCC or RGB equivalent.

What I need:

A function that checks an image for its size, then pads the image to fit 280x210 with the image CENTERED and PADDED with the above specified color.  Please do not hard code 280x210, but make them the defaults for this function.  The function should return the image.
0
adamsetzler
Asked:
adamsetzler
3 Solutions
 
Matthew KellyCommented:
While this is possible, see the response here as to a better option: http://codeigniter.com/forums/viewthread/111495/#562712

If you want this for another reason other than display and the div method does not work for you, let me know.
0
 
adamsetzlerAuthor Commented:
Thanks for the idea, but, unfortunately, I really do need constant dimensions on the images themselves. :/

Could you post what you believe is the entirety of Leon Stafford's code?
0
 
Pedro ChagasWebmasterCommented:
Hi @matt, can you attach a example of the image you want? (padding, centered etc)
Just one example!
Imagemagick and gd are very specific, so, I want to see a example of the final image (do in photoshop or similar software).

Regards, JC
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
adamsetzlerAuthor Commented:
Oh, I'm the one with the question.  :)  Here's an attached example...
ee-example-all.gif
0
 
Pedro ChagasWebmasterCommented:
Hi @matt, I have some doubts.
The function will receive two kind of image orientation, some images with width > height and others width < height, correct?
If above is correct, if the image have exactly  280x210 you don't need padding. In the second image (width < height), I suppose height = 210, but width is like 150, and you want padding 50% (in this case 280-150=130. 130/2=65) for each side, correct?
In last want I assume the real image have (w)210*(h)120.

Please Confirm my idea about your idea!

Regards, JC
0
 
designersxCommented:
will u store the image in a database?

the image u will exract from the website can of any size.

i developed a code which will make any of the image to the particular size. please tell me the above queries if u need them or not, accordingly i need to modify my code.
0
 
adamsetzlerAuthor Commented:
joaochagas and designersx

The images could be of any size, but all of them will fit within 280x210.  One could be 240x210, while another could be 80x200.

I do not need any database interfacing.

The resulting image should have the original image centered horizontally and vertically inside the new image with padding of #CCCCCC in the background.

The final product should be 280x210.

Please use the following syntax for the function, where $image is an image resource:

function_name($image, $color="value", $x=280, $y=210)
{
  do_stuff();
 
  return $image;
}

Open in new window

0
 
Pedro ChagasWebmasterCommented:
Hi @matt, view the snippet code. I create the function magick. $imagename is the image you want work.
You have to create a background with 280*210 (the color you choise), and the name is back.jpg.
You can also change the path of the images, like ...... $imagename = 'folder_name/image.jpg';

If you have any doubt tell me.

Regards, JC
<?
function magick($imagename)
{
$size = getimagesize("$imagename");
if($size[0] > 280 || $size[1] > 210){
exec("/usr/local/bin/convert $imagename -resize 280x210 $imagename");
}
exec("/usr/local/bin/convert back.jpg \\
\( image.jpg -background none  \) \\
-gravity center -composite final.jpg ");
}
$imagename = 'image.jpg';
magick($imagename);
?>
<img src="final.jpg">

Open in new window

0
 
Pedro ChagasWebmasterCommented:
In my server I use this statement for call the convert function of imagemagick, like "exec("/usr/local/bin/convert". Maybe your server is different, check first, and do the changes for your server.

Regards, JC
0
 
adamsetzlerAuthor Commented:
Confused...

Should this:
 
exec("/usr/local/bin/convert back.jpg \\
\( image.jpg -background none  \) \\
-gravity center -composite final.jpg ");
}
 
Be this?
 
exec("/usr/local/bin/convert back.jpg \\
\( $imagename -background none  \) \\
-gravity center -composite $imagename ");
}

Open in new window

0
 
adamsetzlerAuthor Commented:
None of the answers I received were presented in the format that I specified, but they did inspire the following solution:

function pad_image($image, $color='#eee', $dimensions=array(280, 210), $path_to_im='/usr/bin/convert')
{
	if($user->uid == 1) // Debug if admin
	{
		drupal_set_message($image);
	} // End debug
	$size = getimagesize($image);
	if($size[0] < $dimensions[0] || $size[1] < $dimensions[1])
	{
		exec($path_to_im." ".$image." -background '".$color."' -gravity center -extent ".$dimensions[0]."x".$dimensions[1]." ".$image);
	}
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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