Jump to content

[SOLVED] Thumbnails, borders and GD


Schlo_50

Recommended Posts

Hi guys,

 

I have a question that I need answering, but cannot find the solution using Google. Im not sure if it's because I don't know the technical term for it or whether its something not many sites cover. Either way im sure one of the gurus here can help.

 

I have a shopping cart which I use for commerce. The 'add new item' module allows me to upload an image of the item im selling. When an image is uploaded it creates a thumbnail which will be availble for viewing when a visitor is browsing the website.

 

The problem im having is that my php and gd creates a thumbnail and keeps the aspect ratio (which is a good thing, to a degree) so when I have around 20 products on a page they all look un-aligned because of the size of the original images. What I want to do is somehow add a border or frame around the thumbs so that they appear to be aligned within their cells.

 

An example of what im after is here: http://shop.oakleas.co.uk/epages/es110305.sf/en_GB/?ObjectPath=/Shops/es110305_shop

 

I am hoping that this is something that can be implemented into my existing code which is below. Can somebody help me please? Im really stuck on this one!

 

Thanks in advance.

 

/*Begin Generate Thumbnail*/

$imagefile = "uploads/$file_name";
$max_height = 150;
$max_width = 150;
$folder = "thumbs";
$newname = $pid;
$newname = "$newname.jpg";

$size = getimagesize($imagefile);
$width = $size[0];
$height = $size[1];

if($width>$max_width){
  $set_width = $max_width;
  $set_height = round($set_width*($height / $width));
}
elseif($height>$max_height) {
  $set_height = $max_height;
  $set_width = round($set_height*($width / $height));
}
else {
  $set_height = $height;
  $set_width = $width;
}

$image = ImageCreateFromJpeg($imagefile);
$new_image = ImageCreateTrueColor($set_width, $set_height);

ImageCopyResampled($new_image,$image,0,0,0,0,$set_width,$set_height,$width,$height);
imagejpeg($new_image, $folder."/".$newname, 90);

if(trim($_POST['category2']) != "Please Select (optional)"){

$afolder = "thumbs";
$bfolder = "uploads";
$anewname = $pid+1;
$anewname = "$anewname.jpg";

$aimage = ImageCreateFromJpeg($imagefile);
$anew_image = ImageCreateTrueColor($set_width, $set_height);

ImageCopyResampled($anew_image,$aimage,0,0,0,0,$set_width,$set_height,$width,$height);
imagejpeg($anew_image, $afolder."/".$anewname, 90);

$bimage = ImageCreateFromJpeg($imagefile);
$bnew_image = ImageCreateTrueColor($width, $height);

ImageCopyResampled($bnew_image,$bimage,0,0,0,0,$width,$height,$width,$height);
imagejpeg($bnew_image, $bfolder."/".$anewname, 90);

}


/*End Generate Thumbnail*/

Link to comment
https://forums.phpfreaks.com/topic/106591-solved-thumbnails-borders-and-gd/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.