Hey..
Jeg er ved at lave et billed upload system.
Jeg har følgende kode:
$counter = 0;
// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/x-png' => 'png'
);
// GD Function List
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/gif' => 'GIF',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG'
);
// Fetch the photo array sent by preupload.php
$photos_uploaded = $_FILES['photo_filename'];
// Fetch the photo caption array
$photo_caption = $_POST['photo_caption'];
$photo_category = $_POST['category'];
$number_of_fields = $_POST['number_of_fields'];
$path = realpath($_SERVER['DOCUMENT_ROOT']) . $settings['galleryImageDir'];
if (empty($photo_category))
{
$form_error = $error->form_error($error = 'Du har ikke valgt en kategori');
$this->upload();
return;
}
while( $counter <= count($photos_uploaded) )
{
if($photos_uploaded['size'][$counter] > 0)
{
if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
{
$this->output = $error->error('Ugyldig filtype i blandt de uploadede billeder');
}
else
{
$connector->query('INSERT INTO cms_gallery_photos SET photo_filename="0", photo_caption="'.addslashes($photo_caption[$counter]).'",
photo_category="'.addslashes($_POST['category']).'"');
$new_id = mysql_insert_id();
$filetype = $photos_uploaded['type'][$counter];
$extention = $known_photo_types[$filetype];
$filename = $new_id.".".$extention;
$connector->query('UPDATE cms_gallery_photos SET photo_filename="'.addslashes($filename).'" WHERE photo_id="'.addslashes($new_id).'"');
// Store the orignal file
move_uploaded_file($photos_uploaded['tmp_name'][$counter], $path . $filename);
// Let's get the Thumbnail size
$size = GetImageSize( $path . $filename );
if($size[0] > $size[1])
{
$thumbnail_width = 150;
$thumbnail_height = (int)(150 * $size[1] / $size[0]);
}
else
{
$thumbnail_width = (int)(150 * $size[0] / $size[1]);
$thumbnail_height = 150;
}
// Build Thumbnail with GD 1.x.x, you can use the other described methods too
$function_suffix = $gd_function_suffix[$filetype];
$function_to_read = "ImageCreateFrom".$function_suffix;
$function_to_write = "Image".$function_suffix;
// Read the source file
$source_handle = $function_to_read ( $path . $filename );
if($source_handle)
{
// Let's create an blank image for the thumbnail
$destination_handle = imagecreatetruecolor( $thumbnail_width, $thumbnail_height );
// Now we resize it
imagecopyresampled( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] );
}
// Let's save the thumbnail
$function_to_write( $destination_handle, $path."thumbs/tb_".$filename );
ImageDestroy($destination_handle );
//
//Let's make the small images
$size = GetImageSize( $path . $filename );
if($size[0] > $size[1])
{
$small_width = 552;
$small_height = (int)(552 * $size[1] / $size[0]);
}
else
{
$small_width = (int)(552 * $size[0] / $size[1]);
$small_height = 552;
}
// Build small with GD 1.x.x, you can use the other described methods too
$function_suffix = $gd_function_suffix[$filetype];
$function_to_read = "ImageCreateFrom".$function_suffix;
$function_to_write = "Image".$function_suffix;
// Read the source file
$source_handle = $function_to_read ( $path . $filename );
if($source_handle)
{
// Let's create an blank image for the small
$destination_handle = imagecreatetruecolor( $small_width, $small_height );
// Now we resize it
imagecopyresampled( $destination_handle, $source_handle, 0, 0, 0, 0, $small_width, $small_height, $size[0], $size[1] );
}
// Let's save the small
$function_to_write( $destination_handle, $path."small/s_".$filename );
ImageDestroy($destination_handle );
$result_final .= "<img src='".$path."tb_".$filename."' /> File ".($counter+1)." Added<br />";
$this->output = $this->html->add_complete($result_final);
}
}
$counter++;
}
Når jeg uploader et billed, vil jeg have den til at lave et thumbnail, et "small" billed på 552 px i bredden, samt at gemme det originale billed.
Når jeg uploader billeder der er op til 1280 i bredden, virker det fint, men når jeg så prøver at uploade et større billed, gemmer den det originale og et thumbnail. Men "small" billedet fejler og bliver ikke gemt.
Jeg har prøvet at kigge på koden men jeg kan ikke lige finde fejlen, så jeg håber du/i kan.