Jump to content

[SOLVED] PHP Thumbnail Creation


Fabis94

Recommended Posts

Ok so i use this function to create thumbnails:

 

function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode('.',$name);
$src_img=imagecreatefrompng($name);
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) 
{
	$thumb_w=$new_w;
	$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) 
{
	$thumb_w=$old_x*($new_w/$old_y);
	$thumb_h=$new_h;
}
if ($old_x == $old_y) 
{
	$thumb_w=$new_w;
	$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
if (preg_match("/png/",$system[1]))
{
	imagepng($dst_img,$filename); 
} else {
	imagejpeg($dst_img,$filename); 
}
imagedestroy($dst_img); 
imagedestroy($src_img); 
}

 

It works and all, but the problem is that for my gallery i need all the pictures to be exactly 150x150, but i also need the images to keep their aspect ratio (so they don't look ugly) so i was thinking i could add like borders that would fill up the space. Ok imagine the function resizes the image to 100x150 so i make the image centered and add 50px wide white black borders on each side of the picture, so it isn't stretched out (keeps it's ratio) and is also 150x150.

 

The question is, how do i do this?

Link to comment
Share on other sites

Hi Fabis94,

 

It would be easier to use CSS to achieve this.  For example, output the image in the normal way inside a DIV with a "class" tag, for example:

 

<div class="imageborder"><img src="image.jpg" alt="Some Image" /></div>

 

Then, in your CSS file have something like:

 

.imageborder {
width: 150px;
height: 150px;
text-align:center;
vertical-align: middle;
border: 1px solid #000000;
}

 

Something like that should work.

 

Hope this helps.

Link to comment
Share on other sites

Hi Fabis94,

 

It would be easier to use CSS to achieve this.  For example, output the image in the normal way inside a DIV with a "class" tag, for example:

 

<div class="imageborder"><img src="image.jpg" alt="Some Image" /></div>

 

Then, in your CSS file have something like:

 

.imageborder {
width: 150px;
height: 150px;
text-align:center;
vertical-align: middle;
border: 1px solid #000000;
}

 

Something like that should work.

 

Hope this helps.

 

It would work for me, but the image will be used as a link, so i need the whole area (150x150) to be clickable as the link. If i use your way, only when you click on the image you will be lead to the links destination, not the whole 150x150 area.

 

Also i don't quite understand how imagecopymerge() works (i checked the docs).

Link to comment
Share on other sites

a { width: 150px; height: 150px; overflow: hidden; }

<a href='whateverifelllikedoing' target='momoney'><img src='yourproblem' /></a>

or use the image commands in php to do as suggested and create a colored box and copy the image onto that. you will learn more and go farther to do this. the overflow trick is a shortcut.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.