Forum Moderators: coopster
I'm working on a script to allow users to upload images. I'd like to create thumbnails of the images at the same time.
The initial uploading of the image works fine for any size of image. The creation of a thumbnail would seem to work for very small images, but not for larger images. i.e. as soon as I try to upload a decent sized image (820 kb), it crashes and produces error 500 (internal server error). I've tried several different thumbnail scripts but they all result in the same error.
Any ideas what I should do about this? Should I not be uploading the image and creating the thumbnail at the same time?
Really appreciate any help!
Neil
here's the code:
---the end of the upload script -----------------------
// now let's move the file to its final location and allocate the new filename to it
@move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);
//path to folder
$path = '../../images/uploads/jobs/';
//Image: Full Sized
$src = '../../images/uploads/jobs/'.$top_level_file_name;
// Begin
$img = new imaging;
$img->set_img($src);
$img->set_quality(80);
// Small thumbnail
$img->set_size(200);
$img->save_img($path."small_".$top_level_file_name);
// Baby thumbnail
$img->set_size(50);
$img->save_img($path."baby_".$top_level_file_name);
// Finalize
$img->clear_cache();
--------------------------------------
the images class ------------------------
<?php
// Imaging
class imaging
{
// Variables
var $img_input;
var $img_output;
var $img_src;
var $format;
var $quality = 80;
var $x_input;
var $y_input;
var $x_output;
var $y_output;
var $resize;
// Set image
function set_img($img)
{
// Find format
$ext = strtoupper(pathinfo($img, PATHINFO_EXTENSION));
// JPEG image
if(is_file($img) && ($ext == "JPG" OR $ext == "JPEG"))
{
$this->format = $ext;
$this->img_input = ImageCreateFromJPEG($img);
$this->img_src = $img;
}
// PNG image
elseif(is_file($img) && $ext == "PNG")
{
$this->format = $ext;
$this->img_input = ImageCreateFromPNG($img);
$this->img_src = $img;
}
// GIF image
elseif(is_file($img) && $ext == "GIF")
{
$this->format = $ext;
$this->img_input = ImageCreateFromGIF($img);
$this->img_src = $img;
}
// Get dimensions
$this->x_input = imagesx($this->img_input);
$this->y_input = imagesy($this->img_input);
}
// Set maximum image size (pixels)
function set_size($size = 100)
{
// Resize
if($this->x_input > $size && $this->y_input > $size)
{
// Wide
if($this->x_input >= $this->y_input)
{
$this->x_output = $size;
$this->y_output = ($this->x_output / $this->x_input) * $this->y_input;
}
// Tall
else
{
$this->y_output = $size;
$this->x_output = ($this->y_output / $this->y_input) * $this->x_input;
}
// Ready
$this->resize = TRUE;
}
// Don't resize
else { $this->resize = FALSE; }
}
// Set image quality (JPEG only)
function set_quality($quality)
{
if(is_int($quality))
{
$this->quality = $quality;
}
}
// Save image
function save_img($path)
{
// Resize
if($this->resize)
{
$this->img_output = ImageCreateTrueColor($this->x_output, $this->y_output);
ImageCopyResampled($this->img_output, $this->img_input, 0, 0, 0, 0, $this->x_output, $this->y_output, $this->x_input, $this->y_input);
}
// Save JPEG
if($this->format == "JPG" OR $this->format == "JPEG")
{
if($this->resize) { imageJPEG($this->img_output, $path, $this->quality); }
else { copy($this->img_src, $path); }
}
// Save PNG
elseif($this->format == "PNG")
{
if($this->resize) { imagePNG($this->img_output, $path); }
else { copy($this->img_src, $path); }
}
// Save GIF
elseif($this->format == "GIF")
{
if($this->resize) { imageGIF($this->img_output, $path); }
else { copy($this->img_src, $path); }
}
}
// Get width
function get_width()
{
return $this->x_input;
}
// Get height
function get_height()
{
return $this->y_input;
}
// Clear image cache
function clear_cache()
{
@ImageDestroy($this->img_input);
@ImageDestroy($this->img_output);
}
}
?>