I have for a couple of years been stymed to understand how to effectively load images (of more than 2MB) and then create thumbnails. My note below on general file uploading was an early hint of some of the system default limitations and I have recently discovered the final limit I offer this as an example of the various missing pieces of information to successfully load images of more than 2MB and then create thumbnails. This particular example assumes a picture of a user is being uploaded and because of browser caching needs a unique number at the end to make the browser load a new picture for review at the time of upload. The overall calling program I am using is a Flex based application which calls this php file to upload user thumbnails.
The secret sauce is:
1. adjust server memory size, file upload size, and post size
2. convert image to standard formate (in this case jpg) and scale
The server may be adjusted with the .htaccess file or inline code. This example has an .htaccess file with file upload size and post size and then inline code for dynamic system memory.
htaccess file:
php_value post_max_size 16M
php_value upload_max_filesize 6M
<?php
function convertPic($img_base, $w_dst, $h_dst, $n_img, $o_img)
{ini_set('memory_limit', '100M'); unlink($img_base.$n_img); unlink($img_base.$o_img);
$new_img = $img_base.$n_img;
$file_src = $img_base."img.jpg"; unlink($file_src);
move_uploaded_file($_FILES['Filedata']['tmp_name'], $file_src);
list($w_src, $h_src, $type) = getimagesize($file_src); $ratio = $w_src/$h_src;
if ($w_dst/$h_dst > $ratio) {$w_dst = floor($h_dst*$ratio);} else {$h_dst = floor($w_dst/$ratio);}
switch ($type)
{case 1: $img_src = imagecreatefromgif($file_src);
break;
case 2: $img_src = imagecreatefromjpeg($file_src);
break;
case 3: $img_src = imagecreatefrompng($file_src);
break;
}
$img_dst = imagecreatetruecolor($w_dst, $h_dst); imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $w_dst, $h_dst, $w_src, $h_src);
imagejpeg($img_dst, $new_img); unlink($file_src); imagedestroy($img_src);
imagedestroy($img_dst);
}
$p_id = (Integer) $_POST[uid];
$ver = (Integer) $_POST[ver];
$delver = (Integer) $_POST[delver];
convertPic("your/file/structure/", 150, 150, "u".$p_id."v".$ver.".jpg", "u".$p_id."v".$delver.".jpg");
?>