So this is what am trying to achieve:
I have a gallery script which creates thumbnails from an image uploaded - I wish to use the same method that is used to create the thumbnails to compress the uploaded image - This is how I thought would be the best way to go about it;
Original function to create the thumbnail:
Code: Select all
function createThumbnail($orgimg, $thumbnailwidth, $thumbnailquality) {
global $path;
$thumbpath = "{$path}thumbs/{$orgimg}";
$orgimg = "{$path}{$orgimg}";
$error = 0;
if (function_exists('imagecreate') && function_exists('imagecopyresized')) {
// Check if thumbnail directory exists. If not try to create it.
if (!is_dir("{$path}thumbs")) {
$oldumask = umask(0);
if (@!mkdir("{$path}thumbs", 0777)) {
$error = "Thumbnail directory could not be created.";
}
umask($oldumask);
}
// Get file size and file type.
if ($error == 0) {
if (!$size = @getimagesize($orgimg)) {
$error = "Size of original image could not be calculated.";
}
}
// Create link to old image.
if ($error == 0) {
switch ($size[2]) {
case 1 :
if (function_exists('imagecreatefromgif')) {
$img = @imagecreatefromgif($orgimg);
if ($img == "") {
$error = "Could not open link to original image.";
}
} else {
$error = "Could not open link to original image.";
}
break;
case 2 :
if (function_exists('imagecreatefromjpeg')) {
$img = @imagecreatefromjpeg($orgimg);
if ($img == "") {
$error = "Could not open link to original image.";
}
} else {
$error = "Could not open link to original image.";
}
break;
case 3 :
if (function_exists('imagecreatefrompng')) {
$img = @imagecreatefrompng($orgimg);
if ($img == "") {
$error = "Could not open link to original image.";
}
} else {
$error = "Could not open link to original image.";
}
break;
default :
$error = "Cannot create thumbnail. Original image is of an unsupported type.";
break;
}
}
// Calculate the dimensions of the new image.
if ($error == 0) {
if (!strstr($thumbnailwidth, "%")) {
if($size[0] > $size[1]) {
$ratio = $size[0]/$thumbnailwidth;
$height = $size[1]/$ratio;
$height = round($height);
$width = $size[0]/$ratio;
} else {
$ratio = $size[1]/$thumbnailwidth;
$width = $size[0]/$ratio;
$width = round($width);
$height = $size[1]/$ratio;
}
} else {
$ratio = str_replace("%", "", $thumbnailwidth)/100;
$width = round($size[0]*$ratio);
$height = round($size[1]*$ratio);
}
}
// Create new image (true colour if available).
if ($error == 0) {
if (function_exists('imagecreatetruecolor')) {
$newimg = imagecreatetruecolor($width, $height);
} else {
$newimg = imagecreate($width, $height);
}
}
// Resample old image over new image.
if ($error == 0) {
if(!function_exists('imagecopyresampled') || !function_exists('imagecreatetruecolor')) {
if (!@imagecopyresized($newimg, $img, 0, 0, 0, 0, $width, $height, $size[0], $size[1])) {
$error = "Could not resize image.";
}
} else {
if (!@imagecopyresampled($newimg, $img, 0, 0, 0, 0, $width, $height, $size[0], $size[1])) {
$error = "Could not resample image.";
}
}
}
// Make the thumbnails, and save files.
if ($error == 0) {
switch ($size[2]) {
case 1:
if (!@imagegif($newimg, $thumbpath)) {
$error = "Could not save thumbnail.";
}
break;
case 2:
if (!@imagejpeg($newimg, $thumbpath, $thumbnailquality)) {
$error = "Could not save thumbnail.";
}
break;
case 3:
if (!@imagepng($newimg, $thumbpath)) {
$error = "Could not save thumbnail.";
}
break;
default :
$error = "Could not create thumbnail. Image type not supported.";
}
}
// Destroy image both links.
@imagedestroy($newimg);
@imagedestroy($img);
} else {
$error = "Image functions not available for thumbnail.";
}
return $error;
}This is the function which adds the image
Code: Select all
function addImage($newtitle, $newdescription, $newcat, $newfromfile) {
global $name, $title, $description, $cat, $allowedfiles, $maxsize, $path, $enableThumbnails, $thumbnailwidth, $thumbnailquality;
$newtitle = strip_tags(stripslashes(trim($newtitle)));
$newdescription = strip_tags(stripslashes(trim($newdescription)));
$newfromfile = strip_tags(stripslashes(trim($newfromfile)));
// If there are no categories configured we add a category
if ($newcat == "NoCatIsAvailable") {
$temp = array("Untitled");
updateCats($temp);
UnSet($temp);
$newcat = 0;
}
// Check if there is a file uploaded. If there is use that if not check if there is something in the "enter file name" field.
if ($newtitle != "") {
if ($_FILES["localfile"]["error"] == 0 || ($newfromfile != "" && file_exists("{$path}{$newfromfile}"))) {
if (IsSet($_FILES["localfile"]["tmp_name"])) {
// A file has been uploaded.
if (in_array(strtolower(getExt($_FILES["localfile"]["name"])), $allowedfiles)) {
if (in_array(strtolower(getExt($_FILES["localfile"]["name"])), $allowedfiles)) {
if (filesize($_FILES["localfile"]["tmp_name"]) <= $maxsize) {
if (@move_uploaded_file($_FILES["localfile"]["tmp_name"], "{$path}{$_FILES["localfile"]["name"]}")) {
// Build and save the xml data file.
$name[] = $_FILES["localfile"]["name"];
$title[] = $newtitle;
$description[] = $newdescription;
$cat[] = $newcat;
if (saveFile() == TRUE) {
if ($_POST["submit"] != "Add Image") {
UnSet($_REQUEST["action"]);
} else {
$_REQUEST["action"] = "add";
}
echo "<p class=\"ok\">Image Added.</p>";
if ($enableThumbnails == TRUE) {
$msg = createThumbnail($_FILES["localfile"]["name"], $thumbnailwidth, $thumbnailquality);
if ($msg == 0) {
echo "<p class=\"ok\">Thumbnail created.</p>";
} else {
echo "<p class=\"alarm\">{$msg}</p>";
}
}
return TRUE;
} else {
echo "<p class=\"alarm\">Error: The script couldn't save the data file.</p>";
return FALSE;
}
} else {
echo "<p class=\"alarm\">Error: The script couldn't move the file.</p>";
return FALSE;
}
} else {
echo "<p class=\"alarm\">Error: The was was too big.</p>";
return FALSE;
}
} else {
echo "<p class=\"alarm\">Error: This type of image is not allowed.</p>";
return FALSE;
}
} else {
echo "<p class=\"alarm\">Error: This type of image is not allowed.</p>";
return FALSE;
}
} else {
// Check if the file name given exists or return error.
if (in_array(strtolower(getExt($newfromfile)), $allowedfiles)) {
if (file_exists("{$path}{$newfromfile}")) {
// Build and save the xml data file.
$name[] = $newfromfile;
$title[] = $newtitle;
$description[] = $newdescription;
$cat[] = $newcat;
if (saveFile() == TRUE) {
if ($_POST["submit"] != "Add Image") {
UnSet($_REQUEST["action"]);
} else {
$_REQUEST["action"] = "add";
}
echo "<p class=\"ok\">Image added.</p>";
if ($enableThumbnails == TRUE) {
$msg = createThumbnail($newfromfile, $thumbnailwidth, $thumbnailquality);
if ($msg == 0) {
echo "<p class=\"ok\">Thumbnail created.</p>";
} else {
echo "<p class=\"alarm\">{$msg}</p>";
}
}
return TRUE;
} else {
echo "<p class=\"alarm\">Error: The script couldn't save the data file.</p>";
return FALSE;
}
} else {
echo "<p class=\"alarm\">Error: The file you tried to link this entry could not be found.</p>";
return FALSE;
}
} else {
echo "<p class=\"alarm\">Error: This type of image is not allowed.</p>";
return FALSE;
}
}
} else {
echo "<p class=\"alarm\">Error: You did not specify a file for upload (or the file was too big) or the file you tried to link do not exist.</p>";
return FALSE;
}
} else {
echo "<p class=\"alarm\">Error: You did not specify a title.</p>";
return FALSE;
}
}Probably too vague for you guys am sorry
Is anyone available to send these files too? I will obviously pay for your services I just need this compression added quickly.
Thanks guys.