Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

GD: Exact Canvas size

Avatar of hankknight
hankknightFlag for Canada asked on
1 Comment1 Solution453 ViewsLast Modified:
I want my PHP/GD code to make a thumbnail that is EXACTLY 200px tall by 200px wide.  

The image should remain proportional. The canvas behind the image should be white.

For example, an image that is 800px wide and 200px tall should be resized to be 200px wide by 50px tall, but with 150px of white space below the image.
 		echo '<img src="'.
 		.'" alt="" />';
function createthumb($imageName,$thumbName,$new_w,$new_h, $typeThumb=null)
   if (function_exists('imagecopyresampled')) {
	$typeImage = strtolower(pathinfo($imageName, PATHINFO_EXTENSION));
	if($typeThumb===null) $typeThumb = strtolower(pathinfo($thumbName, PATHINFO_EXTENSION));
	if($typeImage==='gif' && !function_exists('imagecreatefromgif')) return;
	if ($typeImage==='jpg'||$typeImage==='jpeg')  $src_img=@imagecreatefromjpeg($imageName);
	elseif ($typeImage==='png') $src_img=@imagecreatefrompng($imageName);
	elseif ($typeImage==='gif') $src_img=@imagecreatefromgif($imageName);
	else return $imageName;
	if((($new_w<=0) or !is_numeric($new_w)) || (($old_x<=0) or !is_numeric($old_x)) || (($new_h<=0) or !is_numeric($new_h)) || (($old_y<=0) or !is_numeric($old_y))) return $imageName;
	$thumb_w=(int) ($ratio*$old_x+.5);
	$thumb_h=(int) ($ratio*$old_y+.5);
	if ($typeThumb==='png')
	elseif ($typeThumb==='gif')
	elseif ($typeThumb==='jpg' || $typeThumb==='jpeg')
	else return $imageName;
	return $thumbName;
	else {
	return $imageName;
Avatar of prokvk

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answers