PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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!
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;
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!
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;
}
}
Last edited by Fuzzo on Wed Jun 27, 2007 2:31 pm, edited 1 time in total.
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.
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;
}
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.
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...
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.