Page 1 of 2
Question about calling functions[S] Another problem found!
Posted: Wed Jun 27, 2007 2:04 pm
by Fuzzo
Hello, So I finally got my last problem worked out but a new one has arisen. I feel I know there is a way round this one that is simple enough but I have been trying for a while - So any help would be appreciated!
I use this bit of code to call a function;
Code: Select all
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;
This works fine and dandy - But as a relative newcomer I was abit perplexed. I want to use another function in the same way (I.e Just after it) Like this;
Code: Select all
if ($enableCompress == TRUE) {
$msg = createCompress($_FILES["localfile"]["name"], $maxwidth, $thumbnailquality);
if ($msg == 0) {
echo "Compression Added";
} else {
echo "<p class=\"alarm\">{$msg}</p>";
}
}
What would be the most efficient way of combining both?
Thanks - Hope this makes sense.
Posted: Wed Jun 27, 2007 2:21 pm
by RobertGonzalez
Add that call where you need it and see what it does.
Posted: Wed Jun 27, 2007 2:23 pm
by Fuzzo
It only uses the first unfortunately - I thought that the easy thing would be to try and combine them; But theres probably something wrong with my coding in general!
Thought id add the full function
Code: Select all
function addImage($newtitle, $newdescription, $newcat, $newfromfile) {
global $name, $title, $description, $cat, $allowedfiles, $maxsize, $maxwidth, $orgpath, $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 ($enableCompress == TRUE) {
$msg = createCompress($_FILES["localfile"]["name"], $thumbnailwidth, $thumbnailquality);
if ($msg == 0) {
echo "Compression Added";
} else {
echo "<p class=\"alarm\">{$msg}</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;
}
}
Posted: Wed Jun 27, 2007 2:29 pm
by RobertGonzalez
You cold always call compressThumbnail from inside of createThumbnail...
Posted: Wed Jun 27, 2007 2:32 pm
by Fuzzo
How would I go about that? Thanks
The code is far from self explainatory so heres what createCompress does;
Takes the uploaded image
Compresses it based on $maxwidth
(This makes a smaller version of the Original image, not a thumbnail)
The code uses in createCompress is fine as it works when in place of createThumbnail.
Cheers
Posted: Wed Jun 27, 2007 2:42 pm
by RobertGonzalez
Show the code for createThumbnail()
Posted: Wed Jun 27, 2007 2:47 pm
by Fuzzo
createThumbnail is a similar bit of code to createCompress - Compress just makes a larger compression than the small thumbnail compression. I basically just need them to coincide together in addImage.
Hope this is understandable, thanks for all the help.
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;
}
Posted: Wed Jun 27, 2007 2:52 pm
by RobertGonzalez
Look in the code you posted for the part where the thumbnail is actually made, the right after, compress it using compressThumbnail() on the new image and the parameters that were passed to createThumbnail.
Posted: Wed Jun 27, 2007 2:57 pm
by Fuzzo
Sorry to keep this going am sure I sound like am talking nonsense.
createCompress (Compresses the uploaded image in addImage)
createThumbnail (Creates a thumbnail from the compressed image)
Hope that makes sense :S
Posted: Wed Jun 27, 2007 3:12 pm
by RobertGonzalez
What you have:
createThumbnail - Creates a thumbnail from an original
createCompress - Creates a compressed image from an original
What you want:
createCompress - Compresses the uploaded image in addImage
createThumbnail - Creates a thumbnail from the compressed image
Do I have that right?
Posted: Wed Jun 27, 2007 3:16 pm
by Fuzzo
Thats correct I do not mind if the thumbnail is taken from the new compressed or the original, whichever works!
Am just trying to get them to work side by side really!
Cheers!
Posted: Wed Jun 27, 2007 3:22 pm
by RobertGonzalez
If that is the case, then this little snip of code you provided should work, as long as the parameters that are passed to the functions check are correct...
Code: Select all
<?php
if ($enableCompress == TRUE) {
$msg = createCompress($_FILES["localfile"]["name"], $thumbnailwidth, $thumbnailquality);
if ($msg == 0) {
echo "Compression Added";
} else {
echo "<p class=\"alarm\">{$msg}</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>";
}
}
?>
When you added that code, did it do anything for you?
Posted: Wed Jun 27, 2007 3:26 pm
by Fuzzo
Where abouts should it be added? Inside addImage? Or elsewhere? Cheers.
Posted: Wed Jun 27, 2007 3:50 pm
by RobertGonzalez
I would suspect that you would want to add in at or around the same time that you are calling the compression routing, sort of like you did in the post you made above in which you showed the add code.
Posted: Wed Jun 27, 2007 3:56 pm
by Fuzzo
Wow. Works perfectly. Genius you are sir and I thank you greatly!
I just replaced the original code with what you gave me and it works
Thanks mate, I really appreciate your help!