Love the class, but although the original image is processed successfully, the resized thumbnail is not, if the original image is over 2mb. Smaller images of 500kb or so, process the thumbnail successfully.
<?php
if ((isset($_POST['action']) ? $_POST['action'] : (isset($_GET['action']) ? $_GET['action'] : '')) == 'xhr') {
// ---------- XMLHttpRequest UPLOAD ----------
// we first check if it is a XMLHttpRequest call
if (isset($_SERVER['HTTP_X_FILE_NAME']) && isset($_SERVER['CONTENT_LENGTH'])) {
// we create an instance of the class, feeding in the name of the file
// sent via a XMLHttpRequest request, prefixed with 'php:'
$handle = new Upload('php:'.$_SERVER['HTTP_X_FILE_NAME']);
} else {
// we create an instance of the class, giving as argument the PHP object
// corresponding to the file field from the form
// This is the fallback, using the standard way
$handle = new Upload($_FILES['my_field']);
}
// then we check if the file has been uploaded properly
// in its *temporary* location in the server (often, it is /tmp)
if ($handle->uploaded) {
//$timestamp = date("ymd-His")."_";//uncomment to add timestamp to file name
//$handle->file_name_body_pre = $timestamp;
$handle->Process($dir_dest);
// yes, the file is on the server
// now, we start the upload 'process'. That is, to copy the uploaded file
// from its temporary location to the wanted location
// It could be something like $handle->Process('/home/www/my_uploads/');
// we check if everything went OK
if ($handle->processed) {
// everything was fine !
echo '<p class="result">';
echo ' <b>Photo uploaded successfully.</b><br />';
echo ' File: <a href="'.$dir_pics.'/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a>';
echo ' ('.human_filesize(filesize($handle->file_dst_pathname)).')';
echo '</p>';
echo('<img src="'.$handle->file_dst_pathname.'" class="img-responsive">');
} else {
// one error occured
echo '<p class="result">';
echo ' <b>File not uploaded to the wanted location</b><br />';
echo ' Error: ' . $handle->error;
echo '</p>';
}
//added this... do some resizing:
$handle->image_resize = true;
$handle->image_x = 300;
$handle->image_ratio_y = true;
$handle->file_name_body_add = '_thumb';
$handle->Process($dir_dest);
//and then create the thumb
if ($handle->processed) {
// everything was fine !
echo '<p class="result">';
echo ' <b>Thumbnail created successfully.</b><br />';
//echo ' File: <a href="'.$dir_pics.'/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a>';
//echo ' ('.human_filesize(filesize($handle->file_dst_pathname)).')';
echo '</p>';
} else {
// one error occured
echo '<p class="result">';
echo ' <b>Unable to create thumbnail.</b> Please contact [email protected]<br />';
echo ' Error: ' . $handle->error . '';
echo '</p>';
}
// we delete the temporary files
$handle-> Clean();
} else {
// if we're here, the upload file failed for some reasons
// i.e. the server didn't receive the file
echo '<p class="result">';
echo ' <b>File not uploaded on the server</b><br />';
echo ' Error: ' . $handle->error . '';
echo '</p>';
}
}
if (isset($handle)) {
//echo '<pre>';
//echo($handle->log);
//echo '</pre>';
}
function human_filesize($bytes, $decimals = 1) {
$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}
?>
No errors are thrown.
Only one old error in the logs:
[31-Aug-2015 02:32:48 UTC] PHP Notice: Undefined variable: timestamp in /home/retinacanadaftp/public_html/photobr.org/resizer/oad.phuplp on line 57