e2interactive forums » Report a Bug

Search:

Autogenerate Thumbnails

(3 posts)

  1. blentz
    Member

    First off, this is great software, and really great work done on it thus far.

    It would be really cool if it could autogenerate the thumbnail images if they don't exist. I haven't gone as far as patching the existing PHP source (if not exist then autogenerate), but I've rewritten a portion of the PHP Thumbnail Generator here http://icant.co.uk/articles/phpthumbnails/ in the form of a PHP shell executable to create the thumbnails for E2.

    Note that I don't use the upload system (couldn't get it to work sensibly... chmod 777 is no solution for me), but that I thought the upload system was supposed to do this.

    
    #!/usr/bin/php
    <?php
            # Variables.
            $source_path = \"./images/\";
            $destination_path = \"./imagethumbs/\";
            $thumb_width = 100;
            $thumb_height = 100;
    
            # Loop through source directory.
            $handle = opendir($source_path);
            while(($filename = readdir($handle)) !== false) {
    
                    # Determine source file type.
                    if(preg_match(\"/\.jpg$|\.jpeg$/i\", $source_path.$filename)) {
                            $src_img = imagecreatefromjpeg($source_path.$filename);
                    }
                    elseif(preg_match(\"/\.png$/i\", $source_path.$filename)) {
                            $src_img = imagecreatefrompng($source_path.$filename);
                    }
                    else {
                            continue;
                    }
    
                    # Determine dimensions of thumbnail without effecting aspect ratio.
                    $current_width = imageSX($src_img);
                    $current_height = imageSY($src_img);
                    if ($current_width > $current_height)
                    {
                            $scale_width = $thumb_width;
                            $scale_height = $current_height * ($thumb_height / $current_width);
                    }
                    if ($current_width < $current_height)
                    {
                            $scale_width = $current_width * ($thumb_width / $current_height);
                            $scale_height = $thumb_height;
                    }
                    if ($current_width == $current_height)
                    {
                            $scale_width = $thumb_width;
                            $scale_height = $thumb_height;
                    }
    
                    # Scale the image.
                    $dst_img = ImageCreateTrueColor($scale_width, $scale_height);
                    imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $scale_width, $scale_height, $current_width, $current_height); 
    
                    # Determine destination file type.
                    if(preg_match(\"/\.jpg$|\.jpeg$/i\", $filename)) {
                            imagejpeg($dst_img, $destination_path.$filename);
                    }
                    elseif(preg_match(\"/\.png$/i\", $filename)) {
                            imagepng($dst_img, $destination_path.$filename);
                    }
                    else {
                            return;
                    }
    
                    # Clean up.
                    imagedestroy($dst_img);
                    imagedestroy($src_img);
            }
    
    ?>
    
    Posted 1 year ago #
  2. This is a cool idea, how would you propose to use this? How are you uploading the large images? Just through an FTP program? At what point would you execute this? When the index.php is loaded or just in the admin section of the uploader?

    I like the idea of doing something easier for the uploading, I suppose there has got to be an easier way to upload the photos through the admin without having to use chmod 777, any ideas?

    Posted 1 year ago #
  3. blentz
    Member

    I think when your script goes to load a thumbnail image from the ./imagethumbs directory, and one doesn't exist, it should invoke this script to auto-create the thumb... that way, the user doesn't have to do anything thumbnail wise, they only have to FTP/SFTP/SCP the image files to the ./images directory.

    The apache server httpd generally runs as an unprivileged, non-root user. On Red Hat systems this is the user "apache", and on other systems (OS X?), it is "www".

    If you use the www group:
    chown root.www images
    chmod 775 images

    Or use the www user:
    chown www images
    chmod 755 images

    you can set the folder so that the web server (apache) user can write to it. chmod 777 is dangerous because that last 7 permits write access to all users on the system, when really all you need is write access for the user running the web server.

    Posted 1 year ago #

RSS feed for this topic

Reply

You must log in to post.