saunders1989 Posted February 20, 2010 Share Posted February 20, 2010 ive tried changing a script i use to create a thumbnail into a function and when i upload a file i want to call that function to create a thumbnail and another image called imageviewed or something. this is the function: <?php function thumbnail($image, $maxWidth, $maxHeight) { $pinfo = pathinfo($image); $tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension']; $imageviewed = $pinfo['dirname'].'/viewed/'.$pinfo['filename'].'_viewed.'.$pinfo['extension']; switch($pinfo['extension']) : case "jpg" : case "jpeg" : $fileType = "jpeg"; $imageCreateFunction = "imagecreatefromjpeg"; $imageOutputFunction = "imagejpeg"; break; case "png" : $fileType = "png"; $imageCreateFunction = "imagecreatefrompng"; $imageOutputFunction = "imagepng"; break; case "gif" : $fileType = "gif"; $imageCreateFunction = "imagecreatefromgif"; $imageOutputFunction = "imagegif"; break; endswitch; list($originalWidth, $originalHeight) = getimagesize($image); $x_ratio = $maxWidth / $originalWidth; $y_ratio = $maxHeight / $originalHeight; // check that the new width and height aren't bigger than the original values. // the new values are higher than the original, don't resize or we'll lose quality if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) { $newWidth = $originalWidth; $newHeight = $originalHeight; } else if (($x_ratio * $originalHeight) < $maxHeight) { $newHeight = ceil($x_ratio * $originalHeight); $newWidth = $maxWidth; } else { $newWidth = ceil($y_ratio * $originalWidth); $newHeight = $maxHeight; } $src = $imageCreateFunction($image); $dst = imagecreatetruecolor($newWidth, $newHeight); // Resample imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); // Save image $imageOutputFunction($dst, $tmb_name, $imageviewed); imagedestroy($src); imagedestroy($dst); return true; } ?> and then in my upload form this is how i think i call the function but im not 100% sure: <?php $max_size=5*1024*1024; // Check if a file has been uploaded if(isset($_FILES['uploaded_file']) && preg_match("/image\/jpeg|image\/jpg/i",$_FILES['uploaded_file']['type']) && $_FILES['uploaded_file']['size']<= $max_size) { // Make sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { $target_path = "images/"; $target_path = $target_path . basename( $_FILES['uploaded_file']['name']); if(!file_exists($target_path)){ if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) { function thumbnail($maxheight,$maxwidth,$image) { $maxheight=150; $maxwidth=100; $image = $target_path; $result=$thumbnail($maxheight, $maxwidth, $image); } function thumbnail($maxheight,$maxwidth, $image) { $maxheight=550; $maxwidth=600; $image = $target_path; $result=$thumbnail($maxheight, $maxwidth, $image); } echo "The file ". basename($_FILES['uploaded_file']['name']). " has been uploaded"; $dbLink = new mysqli('localhost', 'root', '', 'gallery'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Gather all required data $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); $size = intval($_FILES['uploaded_file']['size']); $image_path = $dbLink->real_escape_string($target_path); $gallery_type = $dbLink->real_escape_string($_POST['gallery_type']); $desc = $dbLink->real_escape_string($_POST['desc']); $image_path = $dbLink->real_escape_string($target_path); $tmb_name = $dbLink->real_escape_string($tmb_name); //query to insert the data i had gathered into the database $query = "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`, `desc`, `thumbnail_path` ) VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}', '{$desc}', '{$tmb_name}')"; //executes the query $dbLink->query($query); } } else { echo 'A file with the same name exists please change the file name and try again'; } } else { echo 'A file was not sent'; } } else { echo 'The file is too large'; } // Echo a link back to the main page echo '<p>Click <a href="member-index.php">here</a> to go back</p>'; ?> could someone please correct me if im wrong. thanks Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/ Share on other sites More sharing options...
teamatomic Posted February 20, 2010 Share Posted February 20, 2010 If it works its "correct", which is a relative term as there are many ways to do the same thing. If it does not work tell us what does not happen that you feel should or show the error produced. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015257 Share on other sites More sharing options...
saunders1989 Posted February 20, 2010 Author Share Posted February 20, 2010 this is the error im getting: Fatal error: Cannot redeclare thumbnail() (previously declared in C:\wamp\www\Blean_Photos\thumb_save.php:3) in C:\wamp\www\Blean_Photos\add_file.php on line 31 the files that are corrosponding to the error is: add_file: <?php include 'thumb_save.php'; $max_size=5*1024*1024; // Check if a file has been uploaded if(isset($_FILES['uploaded_file']) && preg_match("/image\/jpeg|image\/jpg/i",$_FILES['uploaded_file']['type']) && $_FILES['uploaded_file']['size']<= $max_size) { // Make sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { $target_path = "images/"; $target_path = $target_path . basename( $_FILES['uploaded_file']['name']); if(!file_exists($target_path)){ if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) { function thumbnail($maxheight=100,$maxwidth=150,$image=target_path) { $result=$thumbnail($maxheight, $maxwidth, $image); } function thumbnail($maxheight=500,$maxwidth=650, $image=target_path) { $result=$thumbnail($maxheight, $maxwidth, $image); } echo "The file ". basename($_FILES['uploaded_file']['name']). " has been uploaded"; $dbLink = new mysqli('localhost', 'root', '', 'gallery'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Gather all required data $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); $size = intval($_FILES['uploaded_file']['size']); $image_path = $dbLink->real_escape_string($target_path); $gallery_type = $dbLink->real_escape_string($_POST['gallery_type']); $desc = $dbLink->real_escape_string($_POST['desc']); $image_path = $dbLink->real_escape_string($target_path); $tmb_name = $dbLink->real_escape_string($tmb_name); //query to insert the data i had gathered into the database $query = "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`, `desc`, `thumbnail_path` ) VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}', '{$desc}', '{$tmb_name}')"; //executes the query $dbLink->query($query); } } else { echo 'A file with the same name exists please change the file name and try again'; } } else { echo 'A file was not sent'; } } else { echo 'The file is too large'; } // Echo a link back to the main page echo '<p>Click <a href="member-index.php">here</a> to go back</p>'; ?> and thumb_save: <?php function thumbnail($image, $maxWidth, $maxHeight) { $pinfo = pathinfo($image); $tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension']; $imageviewed = $pinfo['dirname'].'/viewed/'.$pinfo['filename'].'_viewed.'.$pinfo['extension']; switch($pinfo['extension']) : case "jpg" : case "jpeg" : $fileType = "jpeg"; $imageCreateFunction = "imagecreatefromjpeg"; $imageOutputFunction = "imagejpeg"; break; case "png" : $fileType = "png"; $imageCreateFunction = "imagecreatefrompng"; $imageOutputFunction = "imagepng"; break; case "gif" : $fileType = "gif"; $imageCreateFunction = "imagecreatefromgif"; $imageOutputFunction = "imagegif"; break; endswitch; list($originalWidth, $originalHeight) = getimagesize($image); $x_ratio = $maxWidth / $originalWidth; $y_ratio = $maxHeight / $originalHeight; // check that the new width and height aren't bigger than the original values. // the new values are higher than the original, don't resize or we'll lose quality if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) { $newWidth = $originalWidth; $newHeight = $originalHeight; } else if (($x_ratio * $originalHeight) < $maxHeight) { $newHeight = ceil($x_ratio * $originalHeight); $newWidth = $maxWidth; } else { $newWidth = ceil($y_ratio * $originalWidth); $newHeight = $maxHeight; } $src = $imageCreateFunction($image); $dst = imagecreatetruecolor($newWidth, $newHeight); // Resample imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); // Save image $imageOutputFunction($dst, $tmb_name, $imageviewed); imagedestroy($src); imagedestroy($dst); return true; } ?> i have a feeling i havent sorted out the directorys bit properlly. as im calling image twice and im never calling image_viewed i think it wont go into the right directorys. could you please advise thanks Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015267 Share on other sites More sharing options...
saunders1989 Posted February 20, 2010 Author Share Posted February 20, 2010 i never actually said what i want it to do. basically when i upload an image i want a thumbnail to be created and another image so when the user clicks on the thumbnail a image of a certain height/width is shown instead of resizing the image on the fly. i am struggling to get this to work and not sure how to. Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015268 Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 You can't declare function with same identifier ( thumbnail() ), more than once. You have it declared three times. Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015269 Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 Instead of this in add_file.php function thumbnail($maxheight=100,$maxwidth=150,$image=target_path) { $result=$thumbnail($maxheight, $maxwidth, $image); } function thumbnail($maxheight=500,$maxwidth=650, $image=target_path) { $result=$thumbnail($maxheight, $maxwidth, $image); } you probably want something like: $result = thumbnail( $target_path,100,150); $result = thumbnail( $target_path,500,650); Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015270 Share on other sites More sharing options...
teamatomic Posted February 20, 2010 Share Posted February 20, 2010 This or something similar might be better. $maxheight=100; $maxwidth=150; $image='target_path/thumb_pic.ext'; $result_thumb=$thumbnail($maxheight, $maxwidth, $image); $maxheight=500; $maxwidth=650; $image='target_path/full_pic.ext'; $result_fullsize=$thumbnail($maxheight, $maxwidth, $image); HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015274 Share on other sites More sharing options...
saunders1989 Posted February 20, 2010 Author Share Posted February 20, 2010 i tried this : function thumbnail($maxheight,$maxwidth,$image) { $maxheight=100; $maxwidth=150; $image='target_path/thumbs/thumb_pic.ext'; $result_thumb=$thumbnail($maxheight, $maxwidth, $image); } function thumbnail($maxheight,$maxwidth,$image) { $maxheight=500; $maxwidth=650; $image='target_path/thumb_pic.ext'; $result_thumb=$thumbnail($maxheight, $maxwidth, $image); } but came out with the same fatal error. could you explain to me if the thumbnail image when uploaded would go into the directory in image called thumbs. i know the path would be images/thumbs but how would i do that? Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015284 Share on other sites More sharing options...
teamatomic Posted February 20, 2010 Share Posted February 20, 2010 Try the example posted by Mchl, I missed that he had target_path as a var, sorry. I did notice something else function thumbnail($image, $maxWidth, $maxHeight) but you try $result=thumbnail($maxheight, $maxwidth, $image); note the order listed in the function declaration, you need ot stick with that order when you call the function. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015289 Share on other sites More sharing options...
saunders1989 Posted February 20, 2010 Author Share Posted February 20, 2010 i just keep getting this error: Fatal error: Cannot redeclare thumbnail() (previously declared in C:\wamp\www\Blean_Photos\thumb_save.php:3) in C:\wamp\www\Blean_Photos\add_file.php on line 31 my code is this now: function thumbnail($image, $maxWidth, $maxHeight) { $result = thumbnail($target_path,100,150); } function thumbnail($image, $maxWidth, $maxHeight) { $result = thumbnail( $target_path,500,650); } Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015293 Share on other sites More sharing options...
Mchl Posted February 20, 2010 Share Posted February 20, 2010 You're trying to redeclare (as in: declare again) same function, instead of calling (as in: using) it. See my previous post. Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015294 Share on other sites More sharing options...
saunders1989 Posted February 20, 2010 Author Share Posted February 20, 2010 okay i really have screwed things up. i havent a clue how to do it and im just getting everything wrong. my function i want is to shrink two images on upload and one to a thumbnail size and one to be viewed when you click the thumbnail instead of resizing it on the fly. this is the script im trying to turn into a function: <?php function thumbnail($image, $maxWidth, $maxHeight) { $pinfo = pathinfo($image); $tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension']; $imageviewed = $pinfo['dirname'].'/viewed/'.$pinfo['filename'].'_viewed.'.$pinfo['extension']; switch($pinfo['extension']) : case "jpg" : case "jpeg" : $fileType = "jpeg"; $imageCreateFunction = "imagecreatefromjpeg"; $imageOutputFunction = "imagejpeg"; break; case "png" : $fileType = "png"; $imageCreateFunction = "imagecreatefrompng"; $imageOutputFunction = "imagepng"; break; case "gif" : $fileType = "gif"; $imageCreateFunction = "imagecreatefromgif"; $imageOutputFunction = "imagegif"; break; endswitch; list($originalWidth, $originalHeight) = getimagesize($image); $x_ratio = $maxWidth / $originalWidth; $y_ratio = $maxHeight / $originalHeight; // check that the new width and height aren't bigger than the original values. // the new values are higher than the original, don't resize or we'll lose quality if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) { $newWidth = $originalWidth; $newHeight = $originalHeight; } else if (($x_ratio * $originalHeight) < $maxHeight) { $newHeight = ceil($x_ratio * $originalHeight); $newWidth = $maxWidth; } else { $newWidth = ceil($y_ratio * $originalWidth); $newHeight = $maxHeight; } $src = $imageCreateFunction($image); $dst = imagecreatetruecolor($newWidth, $newHeight); // Resample imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); // Save image $imageOutputFunction($dst, $tmb_name, $imageviewed); imagedestroy($src); imagedestroy($dst); return true; } ?> ive added that variable imageviewed but i dont know if thats correct. when i upload an image i want the thumbnail to go to the folder called thumbs and the viewable image to go to viewed. i then want to run this function in my upload file so when it uploads it automatically creates a thumbnail and an image that goes into the viewd folder. do you want to see my upload form as it is now? Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015300 Share on other sites More sharing options...
teamatomic Posted February 20, 2010 Share Posted February 20, 2010 this is the start of you file with the function <?php function thumbnail($image, $maxWidth, $maxHeight) { add this right after the <?php $image='/path/to/a/valid/original/full/sized/image/image_name.jpg'; thumbnail($image,140,90); Call the file from your browser. What happens? Does it create a thumbnail of the original? If not what is the error. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015311 Share on other sites More sharing options...
saunders1989 Posted February 20, 2010 Author Share Posted February 20, 2010 im really sorry but im new to php and dont understand what youve asked me to do. im pretty sure i dont need to run the file to know it wont create a thumbnail as i have to upload a file first to use the function. ill explain showing you the code what i want to do. behind my upload form i have this add_file.php: ?php $max_size=5*1024*1024; // Check if a file has been uploaded if(isset($_FILES['uploaded_file']) && preg_match("/image\/jpeg|image\/jpg/i",$_FILES['uploaded_file']['type']) && $_FILES['uploaded_file']['size']<= $max_size) { // Make sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { $target_path = "images/"; $target_path = $target_path . basename( $_FILES['uploaded_file']['name']); if(!file_exists($target_path)){ if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) { $image = $target_path; $maxHeight = 90; $maxWidth = 150; include 'thumb_save.php'; // Save a thumb of uploaded pic echo "The file ". basename($_FILES['uploaded_file']['name']). " has been uploaded"; $dbLink = new mysqli('localhost', 'root', '', 'gallery'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Gather all required data $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); $size = intval($_FILES['uploaded_file']['size']); $image_path = $dbLink->real_escape_string($target_path); $gallery_type = $dbLink->real_escape_string($_POST['gallery_type']); $desc = $dbLink->real_escape_string($_POST['desc']); $image_path = $dbLink->real_escape_string($target_path); $tmb_name = $dbLink->real_escape_string($tmb_name); //query to insert the data i had gathered into the database $query = "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`, `desc`, `thumbnail_path` ) VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}', '{$desc}', '{$tmb_name}')"; //executes the query $dbLink->query($query); } } else { echo 'A file with the same name exists please change the file name and try again'; } } else { echo 'A file was not sent'; } } else { echo 'The file is too large'; } // Echo a link back to the main page echo '<p>Click <a href="member-index.php">here</a> to go back</p>'; ?> when it checks to see if the file doesnt exist it sets a maxheight and maxwdith and runs thumb_save. that creates a thumbnail and stores it in a folder called thumbs. which is a sub directory of images. this is thumb_save: <?php // $image, $maxHeight, $maxWidth $pinfo = pathinfo($image); $tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension']; if(!isset($maxWidth)){ $maxWidth = 100; } if(!isset($maxHeight)){ $maxHeight = 150; } switch(strtolower(substr($image, -3))) { case "jpg" : $fileType = "jpeg"; $imageCreateFunction = "imagecreatefromjpeg"; $imageOutputFunction = "imagejpeg"; break; case "jpeg" : $fileType = "jpeg"; $imageCreateFunction = "imagecreatefromjpeg"; $imageOutputFunction = "imagejpeg"; break; case "png" : $fileType = "png"; $imageCreateFunction = "imagecreatefrompng"; $imageOutputFunction = "imagepng"; break; case "gif" : $fileType = "gif"; $imageCreateFunction = "imagecreatefromgif"; $imageOutputFunction = "imagegif"; break; } $size = GetImageSize($image); $originalWidth = $size[0]; $originalHeight = $size[1]; $x_ratio = $maxWidth / $originalWidth; $y_ratio = $maxHeight / $originalHeight; // check that the new width and height aren't bigger than the original values. // the new values are higher than the original, don't resize or we'll lose quality if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) { $newWidth = $originalWidth; $newHeight = $originalHeight; } else if (($x_ratio * $originalHeight) < $maxHeight) { $newHeight = ceil($x_ratio * $originalHeight); $newWidth = $maxWidth; } else { $newWidth = ceil($y_ratio * $originalWidth); $newHeight = $maxHeight; } $src = $imageCreateFunction($image); $dst = imagecreatetruecolor($newWidth, $newHeight); // Resample imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); // Save image $imageOutputFunction($dst, $tmb_name); ImageDestroy($src); ImageDestroy($dst); ?> i then need to make thumb_save into a function so i can create a thumbnail and an image the user can view when clicked on the thumbnail so i need to change my maxheight, maxwidth and the directory where the files go. Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015319 Share on other sites More sharing options...
teamatomic Posted February 20, 2010 Share Posted February 20, 2010 Take the code below and put it in a file. Name it whatever.php note the additions I asked you to make at the start of the file. 1.make sure the path refers to a valid large sized image. 2.make sure that the folder referred to right before the file name has a sub folder named "thumbs" in this example /home/test/thumbs 3.if you have to upload the file, to your server, do so 4. if you have to upload the original image do so. 5. in your browser call the file. ie http://domain.com/whatever.php <?php ############################## $image='/home/test/test.jpg';########### thumbnail($image,140,90);############ ############################# function thumbnail($image, $maxWidth, $maxHeight) { $pinfo = pathinfo($image); $tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension']; $imageviewed = $pinfo['dirname'].'/viewed/'.$pinfo['filename'].'_viewed.'.$pinfo['extension']; switch($pinfo['extension']) : case "jpg" : case "jpeg" : $fileType = "jpeg"; $imageCreateFunction = "imagecreatefromjpeg"; $imageOutputFunction = "imagejpeg"; break; case "png" : $fileType = "png"; $imageCreateFunction = "imagecreatefrompng"; $imageOutputFunction = "imagepng"; break; case "gif" : $fileType = "gif"; $imageCreateFunction = "imagecreatefromgif"; $imageOutputFunction = "imagegif"; break; endswitch; list($originalWidth, $originalHeight) = getimagesize($image); $x_ratio = $maxWidth / $originalWidth; $y_ratio = $maxHeight / $originalHeight; // check that the new width and height aren't bigger than the original values. // the new values are higher than the original, don't resize or we'll lose quality if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) { $newWidth = $originalWidth; $newHeight = $originalHeight; } else if (($x_ratio * $originalHeight) < $maxHeight) { $newHeight = ceil($x_ratio * $originalHeight); $newWidth = $maxWidth; } else { $newWidth = ceil($y_ratio * $originalWidth); $newHeight = $maxHeight; } $src = $imageCreateFunction($image); $dst = imagecreatetruecolor($newWidth, $newHeight); // Resample imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); // Save image $imageOutputFunction($dst, $tmb_name, $imageviewed); imagedestroy($src); imagedestroy($dst); return true; } ?> The reason we are doing this is you have a few files you dont really understand at all. You have no idea what works and what does not. Therefore we start at the beginning and debug. The first thing to do is make sure the function does work((it does)but not as you think). Then we will move on to the next step of fixing your function to something that will work for you. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015331 Share on other sites More sharing options...
saunders1989 Posted February 21, 2010 Author Share Posted February 21, 2010 the thing is i dont want to specify an actualy image. i want to do this function on upload so i dont have a location for the image it just does it when i upload the file. Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015574 Share on other sites More sharing options...
saunders1989 Posted February 21, 2010 Author Share Posted February 21, 2010 when i try and run the file using a test image like the one at the top of the file i get this error: Warning: imagejpeg() expects parameter 3 to be long, string given in C:\wamp\www\Blean_Photos\testing_thumb_save_function.php on line 92 Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015578 Share on other sites More sharing options...
mattal999 Posted February 21, 2010 Share Posted February 21, 2010 I fixed the above error, and added a few comments so you can understand. By using function thumbnail() {, you are defining the function. Now you've defined it, you call it using the function name followed by the variables (see line 2 - thumbnail($image, 140, 90)). <?php $image='/home/test/test.jpg'; // You can change this when you can clarify it works. thumbnail($image, 140, 90); // Just to clarify, once this file is included in another PHP script, you use this to call the function. function thumbnail($image, $maxWidth, $maxHeight) { $pinfo = pathinfo($image); $tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension']; $quality = 100; // imagejpeg() accepts quality as the third variable, not a path. switch($pinfo['extension']) : case "jpg" : case "jpeg" : $fileType = "jpeg"; $imageCreateFunction = "imagecreatefromjpeg"; $imageOutputFunction = "imagejpeg"; break; case "png" : $fileType = "png"; $imageCreateFunction = "imagecreatefrompng"; $imageOutputFunction = "imagepng"; break; case "gif" : $fileType = "gif"; $imageCreateFunction = "imagecreatefromgif"; $imageOutputFunction = "imagegif"; break; endswitch; list($originalWidth, $originalHeight) = getimagesize($image); $x_ratio = $maxWidth / $originalWidth; $y_ratio = $maxHeight / $originalHeight; // check that the new width and height aren't bigger than the original values. // the new values are higher than the original, don't resize or we'll lose quality if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) { $newWidth = $originalWidth; $newHeight = $originalHeight; } else if (($x_ratio * $originalHeight) < $maxHeight) { $newHeight = ceil($x_ratio * $originalHeight); $newWidth = $maxWidth; } else { $newWidth = ceil($y_ratio * $originalWidth); $newHeight = $maxHeight; } $src = $imageCreateFunction($image); $dst = imagecreatetruecolor($newWidth, $newHeight); // Resample imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); // Save image $imageOutputFunction($dst, $tmb_name, $quality); imagedestroy($src); imagedestroy($dst); return true; } ?> Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015582 Share on other sites More sharing options...
saunders1989 Posted February 21, 2010 Author Share Posted February 21, 2010 thanks for replying mattall. can i explain what i need this function to do. at the moment it creates a thumbnail and places that in a folder called thumbs. i need the function to do that but i also need the function to create another image for example at 350x300 and save that image into a folder called viewed. i dont know how to go about that and i dont know how i would calls this file in my upload form Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015583 Share on other sites More sharing options...
mattal999 Posted February 21, 2010 Share Posted February 21, 2010 Ok. Here is code that should work: add_file.php <?php $max_size=5*1024*1024; // Check if a file has been uploaded if(isset($_FILES['uploaded_file']) && preg_match("/image\/jpeg|image\/jpg/i",$_FILES['uploaded_file']['type']) && $_FILES['uploaded_file']['size']<= $max_size) { // Make sure the file was sent without errors if($_FILES['uploaded_file']['error'] == 0) { $target_path = "images/"; $target_path = $target_path . basename( $_FILES['uploaded_file']['name']); if(!file_exists($target_path)){ if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) { // --- IMPORTANT --- // $image = $target_path; $maxHeight = 90; $maxWidth = 150; include 'thumbnail_function.php'; // Include the function file thumbnail($image, $maxWidth, $maxHeight); // Call the function with a width of 90... $maxWidth = 350; $maxHeight = 300; // Redefine the size. thumbnail($image, $maxWidth, $maxHeight); // Call the function again with a width of 350... // --- IMPORTANT --- // echo "The file ". basename($_FILES['uploaded_file']['name']). " has been uploaded"; $dbLink = new mysqli('localhost', 'root', '', 'gallery'); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } // Gather all required data $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']); $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']); $size = intval($_FILES['uploaded_file']['size']); $image_path = $dbLink->real_escape_string($target_path); $gallery_type = $dbLink->real_escape_string($_POST['gallery_type']); $desc = $dbLink->real_escape_string($_POST['desc']); $image_path = $dbLink->real_escape_string($target_path); $tmb_name = $dbLink->real_escape_string($tmb_name); //query to insert the data i had gathered into the database $query = "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`, `desc`, `thumbnail_path` ) VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}', '{$desc}', '{$tmb_name}')"; //executes the query $dbLink->query($query); } } else { echo 'A file with the same name exists please change the file name and try again'; } } else { echo 'A file was not sent'; } } else { echo 'The file is too large'; } // Echo a link back to the main page echo '<p>Click <a href="member-index.php">here</a> to go back</p>'; ?> thumbnail_function.php <?php $image='/home/test/test.jpg'; // You can change this when you can clarify it works. thumbnail($image, 140, 90); // Just to clarify, once this file is included in another PHP script, you use this to call the function. function thumbnail($image, $maxWidth, $maxHeight) { $pinfo = pathinfo($image); $tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension']; $quality = 100; // imagejpeg() accepts quality as the third variable, not a path. switch($pinfo['extension']) : case "jpg" : case "jpeg" : $fileType = "jpeg"; $imageCreateFunction = "imagecreatefromjpeg"; $imageOutputFunction = "imagejpeg"; break; case "png" : $fileType = "png"; $imageCreateFunction = "imagecreatefrompng"; $imageOutputFunction = "imagepng"; break; case "gif" : $fileType = "gif"; $imageCreateFunction = "imagecreatefromgif"; $imageOutputFunction = "imagegif"; break; endswitch; list($originalWidth, $originalHeight) = getimagesize($image); $x_ratio = $maxWidth / $originalWidth; $y_ratio = $maxHeight / $originalHeight; // check that the new width and height aren't bigger than the original values. // the new values are higher than the original, don't resize or we'll lose quality if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) { $newWidth = $originalWidth; $newHeight = $originalHeight; } else if (($x_ratio * $originalHeight) < $maxHeight) { $newHeight = ceil($x_ratio * $originalHeight); $newWidth = $maxWidth; } else { $newWidth = ceil($y_ratio * $originalWidth); $newHeight = $maxHeight; } $src = $imageCreateFunction($image); $dst = imagecreatetruecolor($newWidth, $newHeight); // Resample imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); // Save image $imageOutputFunction($dst, $tmb_name, $quality); imagedestroy($src); imagedestroy($dst); return true; } ?> Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015586 Share on other sites More sharing options...
saunders1989 Posted February 21, 2010 Author Share Posted February 21, 2010 i just have a couple more issues. when i upload the image. a thumbnail gets created and is 350x300. it doesnt create the 90x150. i also need an image to go to a folder called viewed. at the moment it only goes to a folder called thumbs. and the last thing is what would my query be to write the image path of these two images? Link to comment https://forums.phpfreaks.com/topic/192728-is-this-correct/#findComment-1015596 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.