+ Reply to Thread
Results 1 to 7 of 7

Thread: Saving GD thumbs to directory problem

  1. #1
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Saving GD thumbs to directory problem

    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 Code:
    //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$image0000$modwidth$modheight$width$height); 

        
    //imagejpeg($tn, null, 60);
        
        
    $save 'thumbs/'.$image_id.'.jpg';
        
    imagejpeg($tn$save60) ; 
    $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?

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Saving GD thumbs to directory problem

    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);

  3. #3
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Saving GD thumbs to directory problem

    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.

  4. #4
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Saving GD thumbs to directory problem

    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 by lemon-tree; 06-27-2010 at 10:02 AM.

  5. #5
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Saving GD thumbs to directory problem

    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 Code:
    <?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$image0000$modwidth$modheight$width$height);
            
    $new_filename $image_id.".jpg";
            
    $save 'thumbs/'.$new_filename;
            
    imagejpeg($tn$save100);
        }

        
    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);
    ?>

  6. #6
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Saving GD thumbs to directory problem

    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.

  7. #7
    learning_brain is offline x10 Sophmore learning_brain is an unknown quantity at this point
    Join Date
    Apr 2010
    Location
    UK, Midlands
    Posts
    170

    Re: Saving GD thumbs to directory problem

    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 by learning_brain; 06-28-2010 at 04:38 PM.

+ Reply to Thread

Similar Threads

  1. Directory problem?
    By sharlaxismp in forum Free Hosting
    Replies: 3
    Last Post: 11-23-2009, 10:26 PM
  2. Problem with root directory
    By suhaib in forum Programming Help
    Replies: 3
    Last Post: 06-08-2008, 12:46 AM
  3. Protected Directory problem
    By Wonders in forum Free Hosting
    Replies: 3
    Last Post: 11-02-2007, 02:16 PM
  4. Directory permission Problem
    By sourabhj in forum Free Hosting
    Replies: 2
    Last Post: 07-14-2007, 06:15 PM
  5. Problem with www directory alias
    By hazardu5 in forum Free Hosting
    Replies: 3
    Last Post: 03-04-2005, 12:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers