Saving GD thumbs to directory problem

learning_brain

New Member
Messages
206
Reaction score
1
Points
0
Hope you can help on this.

I have a short script to produce a thumb from an image url and then save it to my thumbs directory.

PHP:
//set max size for thumb
    $max_width=110;
    $max_height=80;
    
    //header('Content-type: image/jpeg');
    
    // Setting the resize parameters
    list($width, $height) = getimagesize($image_url);
    
    $ratio = $width/$height;
    
    if($width>$height){
        $modwidth = $max_width;
        $modheight = $max_height / $ratio;
    } else {
        $modheight = $max_height;
        $modwidth = $max_width / $ratio;
    }

    // Resizing the Image
    $tn = imagecreatetruecolor($modwidth, $modheight);
    $image = imagecreatefromjpeg($image_url);
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); 

    //imagejpeg($tn, null, 60);
    
    $save = 'thumbs/'.$image_id.'.jpg';
    imagejpeg($tn, $save, 60) ;

$image_url and $image_id are pulled from a recordset.

I think the script works (and have tested it with the jpg header) but when trying to save, I get ...

Warning: imagejpeg() [function.imagejpeg]: Unable to open 'thumbs/33595.jpg' for writing: Permission denied in /home/a3804641/public_html/test2.php on line 45


I have checked the permissions on the thumbs folder, which are "rwxr-xr-x"

Why am I getting this error message?
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Does the image output to the browser properly if you uncomment the '//imagejpeg($tn, null, 60);' line?
The big question, does the thumbs directory actually exist in the same folder as the test2.php script? If it does then try using a proper full path to it such as:
$save = '/home/a3804641/public_html/thumbs/'.$image_id.'.jpg';
You should also have this at the end of the script to free up memory:
imagedestroy($tn);
imagedestroy($image);
 

learning_brain

New Member
Messages
206
Reaction score
1
Points
0
Thanks lemon-tree

Yes the image did output fine, but after lots of trial and error, I needed to alter the folder priviledges to xwrxwrxwr, which worked fine - strange host!

Thnaks for the tip about the image destroy info.
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
That is peculiar, mine saves fine into a directory with 755, you could probably change it to 775 and still have it work.

Edit: forgot you weren't on x10, sounds like your host has something configured wrong.
 
Last edited:

learning_brain

New Member
Messages
206
Reaction score
1
Points
0
lol - yeah you could say that!

I've been busily working on a thumbnail creator to speed up loading of the page. The original images just filter through in the background for large previews.

My initial script seems to be working very nicely , but it appears some sites won't let me "pinch" the image values so I can wok on them. This I have expected and planned to put an image saying "no thumbnail available", but these are just turning out black....?

+ is there a better way to ensure I get all the thumbs?

code below for reference..

PHP:
<?php
require_once('Connections/freewebhost.php');//connection parameters
mysql_select_db($database_freewebhost, $freewebhost);//select mysql database

$query_images = "SELECT * FROM IMAGES WHERE IMAGETHUMB IS NULL AND SIZECHECK > 0 AND SUIT > 0 ORDER BY ID DESC LIMIT 0,10";
$result_images = mysql_query($query_images, $freewebhost) or die(mysql_error());
$row_images = mysql_fetch_assoc($result_images);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<META HTTP-EQUIV=Refresh CONTENT="1">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Thumb Creator</title>
</head>

<body>

<p><a href='admin.php'>Admin</a></p>

<?php
do{
	$image_url = $row_images['IMAGEURL'];
	$image_id = $row_images['ID'];
	
	$time = time();
	
	$info = pathinfo($image_url);
	$extension = $info['extension'];

	//----------------------------------------------------------------------------
	
	//set max size for thumb
	$max_width=110;
	$max_height=80;

	//header('Content-type: image/jpeg');
	
	// Setting the resize parameters
	list($width, $height) = getimagesize($image_url);
	
	if($width>$height){
		$modwidth = $max_width;
		$factor = $width/$height;
		$modheight = $max_width / $factor;
	} else {
		$modheight = $max_height;
		$factor = $height/$width;
		$modwidth = $max_height / $factor;
	}

	// Resizing the Image
	$tn = imagecreatetruecolor($modwidth, $modheight);
	
	if($extension =="jpg"){
		$image = @imagecreatefromjpeg($image_url);
	} elseif ($extension =="png"){
		$image = @imagecreatefrompng($image_url);
	} elseif ($extension =="gif"){
		$image = @imagecreatefromgif($image_url);
	}
	
	if (!$image) { /* See if it failed */
		$font  = 2;
		$width  = imagefontwidth($font) * strlen($string);
		$height = imagefontheight($font); 
		$image = imagecreatetruecolor (120,80);
		$white = imagecolorallocate ($image,255,255,255);
		$black = imagecolorallocate ($image,0,0,0);
		imagefill($image,0,0,$white);
		imagestring ($image,$font,0,0,"No thumb available",$black);

	} else {
		imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
		$new_filename = $image_id.".jpg";
		$save = 'thumbs/'.$new_filename;
		imagejpeg($tn, $save, 100);
	}

	imagedestroy($tn);
	imagedestroy($image); 

	//----------------------------------------------------------------------------
	
	$updateSQL1 = "UPDATE IMAGES SET IMAGETHUMB='$new_filename' WHERE ID='$image_id'";
	$Result2 = mysql_query($updateSQL1, $freewebhost) or die(mysql_error());
	
	echo "<hr/>";
	
	echo "Image thumb for ".$image_url." created at :".date("d/m/Y H:i:s", $time);
	
	echo "<hr/>";

} while ($row_images = mysql_fetch_assoc($result_images));

?>
</body>
</html>
<?php
mysql_free_result($result_images);
?>
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
I'm not sure whether disabling hotlinking on their sites means you can't access it. Also, there are more image extensions than just jpg, png, gif.
 

learning_brain

New Member
Messages
206
Reaction score
1
Points
0
Sorted this.

You are right, there are many image types not included here such as bmp, ico, tiff to name but a few.... and the hotlinking issue cannot be resolved easily.

In order to avoid this problem, I have created a loop asking if $image exists. If it doesn't, no file is saved and it marks the DB as having tried.

This then led to my next issue which was a mix of thumbs and a few original images. I've just done a check on teh idex page to see if the DB has a record saying that a thumb has been saved. If it has, show that. If not, load the original image.

It's not perfect, but it works well and speeds up the visitor experience massively because each thumb is only 10kb approx @ 100 quality.

Because of the way I've set it up, I can change the thumb creator and then go back through all the unprocessed thumbs to obtain the rest.
 
Last edited:
Top