Jump to content

I want to add thumbnail creation during file upload...how?


simcoweb

Recommended Posts

I have this file upload code and it's working just perfectly. However, i'd love to have either:

1 - preferred choice) it also create a thumbnail at the time of upload and deposit that in a separate folder and enter the image name into the mysql DB

Or,

2) figure out a way to generate a thumbnail 'on the fly' when i'm calling the image to be displayed on a specific page where I need the image to be thumbnail size.

Here's my file upload code that works fine for the general image:

[code]// Upload File
$eg_success_File1 = false;
if(!empty($_FILES['photo']['name']))
{
// Check file is not larger than specified maximum size
$eg_allowUpload = $_FILES['photo']['size'] <= 100000 ? true : false;
// Check file is of the specified type
if($eg_allowUpload)
$eg_allowUpload = preg_match('/\\.(gif|jpg|jpeg|png)$/i', $_FILES['photo']['name']) ? true : false;

if($eg_allowUpload)
{
if(is_uploaded_file($_FILES['photo']['tmp_name']))
{
$eg_uploaddir = $_SERVER['DOCUMENT_ROOT']."/images/photo/";

$eg_uploadFile1 = $eg_uploaddir.rawurlencode($_FILES['photo']['name']);
// Create a unique filename for the uploaded file
$eg_i = 1;
while (file_exists($eg_uploadFile1))
{
$eg_separated_filename = explode(".",$eg_uploadFile1);
if (substr($eg_separated_filename[0],-1) == $eg_i)
{
$eg_separated_filename[0] = substr($eg_separated_filename[0], 0, (strlen($eg_separated_filename[0])-1));
$eg_i++;
}
$eg_separated_filename[0] = $eg_separated_filename[0] . "$eg_i";
$eg_uploadFile1 = implode(".",$eg_separated_filename);
}

$eg_success_File1 = move_uploaded_file($_FILES['photo']['tmp_name'], $eg_uploadFile1);
}

}

}[/code]

I also found this snippet of code that is supposed to do something like choice #2 as i've described:

[code]<?php
# Constants
define(IMAGE_BASE, '/var/www/html/mbailey/images');
define(MAX_WIDTH, 150);
define(MAX_HEIGHT, 150);

# Get image location
$image_file = str_replace('..', '', $_SERVER['QUERY_STRING']);
$image_path = IMAGE_BASE . "/$image_file";

# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
    $img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
    $img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
    $img = @imagecreatefrompng($image_path);
}

# If an image was successfully loaded, test the image for size
if ($img) {

    # Get image size and scale ratio
    $width = imagesx($img);
    $height = imagesy($img);
    $scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);

    # If the image is larger than the max shrink it
    if ($scale < 1) {
        $new_width = floor($scale*$width);
        $new_height = floor($scale*$height);

        # Create a new temporary image
        $tmp_img = imagecreatetruecolor($new_width, $new_height);

        # Copy and resize old image into new image
        imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
                        $new_width, $new_height, $width, $height);
        imagedestroy($img);
        $img = $tmp_img;
    }
}

# Create error image if necessary
if (!$img) {
    $img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
    imagecolorallocate($img,0,0,0);
    $c = imagecolorallocate($img,70,70,70);
    imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
    imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}

# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?> [/code]

This was found here: [url=http://codewalkers.com/tutorials/42/1.html]http://codewalkers.com/tutorials/42/1.html[/url]

If i'm understanding correctly, it creates a thumbnail image 'when needed'. That's an option if I can't find a modestly simple way to modify my upload code to include the creation of the thumbnail while uploading the file.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.