Jump to content

Help with GD Library Script


sailorsmokey

Recommended Posts

It has been approximately 5 years since I've last even looked at GD library scripts. And I don't remember them - at all. I've got this code going, but I can't figure out why it won't copy the image into the correct folder. I'm going to include the section where it's copying the image - maybe someone will see something in there. And if that all checks out, then I guess I'll keep searching the rest of the code for something that's out of place.

 

Absolutely no errors pop up when I run this code. The folder has 777 permissions. But after I run the code, no image appears in the "additionalpix" folder.

 

$dest="/PrivateInfo/additionalpix/$additionalpix_name";
$size = GetImageSize($additionalpix);

$image_types = array(  
'image/pjpeg' => '.jpg',
'image/jpeg' => '.jpg',
'image/jpg' => '.jpg',
'image/gif' => '.gif',
);


$filetype = $_FILES['additionalpix']['type'];
$extension = $image_types[$filetype]; 

//define size based on tall or wide
// Wide Image
if($size[0] > $size[1])
{  
if ($size[0]>250){//only modify it if it's larger
$image_width = 250;  
$image_height = (int)(250 * $size[1] / $size[0]);  
}else{
$image_width = $size[0];  
$image_height = $size[1]; 
}
}  

// Tall Image
else
{
if($size[1]>175){
$image_width = (int)(175 * $size[0] / $size[1]);
$image_height = 175;
}else{
$image_width = $size[0];  
$image_height = $size[1]; 
}
}

$gd_function_suffix = array(  
'.jpg' => 'JPEG',
'.gif' => 'GIF',
);


$function_suffix = $gd_function_suffix[$extension];
$function_to_read = 'ImageCreateFrom'.$function_suffix;
$function_to_write = 'Image' . $function_suffix;

// Read the source file
$source_handle = $function_to_read($additionalpix);
       
if ($source_handle) {
// Let's create a blank image
$destination_handle =
   ImageCreateTrueColor($image_width, $image_height);

// Now we resize it
ImageCopyResampled($destination_handle, $source_handle,
   0, 0, 0, 0, $image_width, $image_height, $size[0], $size[1]);
}

// Let's save
$function_to_write($destination_handle, $dest);

Link to comment
Share on other sites

No, I didn't have the function error_reporting(E_ALL) on, but I just turned it on and ran it.  Again, no error pops up.  And I thought I had the $function_to_write set up correctly, but by your above post, I get the hint that I don't, but I don't know what I did wrong.  I have some examples in other areas of the Web site, and it looks correct compared to them. 

 

If you're feeling generous out there, pass me another hint, and I'll keep digging on this.  Thanks!

Link to comment
Share on other sites

I think that function_to_write thing is part of the GD library, but I'm having trouble piecing this whole thing together.  Here's a tutorial I found on resizing thumbnails with the GD library that uses function_to_write:

 

http://www.sitepoint.com/article/php-gallery-system-minutes/5/

 

  If anyone has any further input on this, I'd love to fix what I already have, but if no one has any other suggestions, I will go with Michael's suggestion of using imagejpeg().  I was just hoping not to have to start over, but again, will do what I have to.

 

Thank you!

Link to comment
Share on other sites

I wrote this a few years back - still use it for resizing images:

 

<?php
ini_set("memory_limit","128M"); //optional - I find it useful if you're expecting really big files to be uploaded
function resize_image($func_im,$width,$height) {
if(imagesx($func_im)>imagesy($func_im)) {
	if(imagesx($func_im)<=$width) {
		$func_newim=imagecreatetruecolor(imagesx($func_im),imagesy($func_im));
		imagecopy($func_newim,$func_im,0,0,0,0,imagesx($func_im),imagesy($func_im));
	} else {
		$func_newim=imagecreatetruecolor($width,((imagesy($func_im)/imagesx($func_im))*$width));
		imagecopyresampled($func_newim,$func_im,0,0,0,0,$width,((imagesy($func_im)/imagesx($func_im))*$width),imagesx($func_im),imagesy($func_im));
	}
} else {
	if(imagesy($func_im)<=$height) {
		$func_newim=imagecreatetruecolor(imagesx($func_im),imagesy($func_im));
		imagecopy($func_newim,$func_im,0,0,0,0,imagesx($func_im),imagesy($func_im));
	} else {
		$func_newim=imagecreatetruecolor(((imagesx($func_im)/imagesy($func_im))*$height),$height);
		imagecopyresampled($func_newim,$func_im,0,0,0,0,((imagesx($func_im)/imagesy($func_im))*$height),$height,imagesx($func_im),imagesy($func_im));	
	}
}
return($func_newim);
}
//function ends - here's how I call it:

do {
if((key($_FILES)!="") AND (strlen(file_get_contents($_FILES[key($_FILES)]['tmp_name'])))) {
	$im=imagecreatefromstring(file_get_contents($_FILES[key($_FILES)]['tmp_name']));
	$newim=resize_image($im,300,240);
	$thumb=resize_image($im,75,75);
	imagejpeg($thumb,"/full/path/to/file/".$_FILES[key($_FILES)]['name'],90);
	imagejpeg($newim,"/full/path/to/file/".$_FILES[key($_FILES)]['tmp_name'],90);
}
}while(each($_FILES));
?>

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.