runei Posted March 6, 2009 Share Posted March 6, 2009 Oy people. I got the following script making thumbnails. It sets the width of the thumbnail but i also want a set height for the thumbnail. Need some help.. <?php ob_start(); error_reporting (E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR); $final_width_of_image = 100; $path_to_image_directory = 'images/fullsized/'; $path_to_thumbs_directory = 'images/thumbs/'; if (isset($_FILES['myphoto'])) { if (preg_match('/[.](jpg)|(JPG)|(JEPG)|(gif)|(png)$/', $_FILES['myphoto']['name'])) { $filename = $_FILES['myphoto']['name']; $source = $_FILES['myphoto']['tmp_name']; $target = $path_to_image_directory . $filename; move_uploaded_file($source, $target); if (preg_match('/[.](jpg)$/', $filename)) { $myimage = imagecreatefromjpeg($path_to_image_directory . $filename); } else if (preg_match('/[.](gif)$/', $filename)) { $myimage = imagecreatefromgif($path_to_image_directory . $filename); } else if (preg_match('/[.](png)$/', $filename)) { $myimage = imagecreatefrompng($path_to_image_directory . $filename); } $width = imagesx($myimage); $height = imagesy($myimage); $finalwidth = $final_width_of_image; $newyvalue = floor($height * ($finalwidth / $width)); $color = imagecreatetruecolor($finalwidth, $newyvalue); imagecopyresampled($color, $myimage, 0, 0, 0, 0, $finalwidth, $newyvalue, $width, $height); if (!file_exists($path_to_thumbs_directory)) { if (!mkdir($path_to_thumbs_directory)) { die("There was a problem. Please try again!"); } } imagejpeg($color, $path_to_thumbs_directory . $filename); $thumb = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />'; #$thumb .= '<br />Congratulations. Your file has been successfully uploaded, and a thumbnail has been created.'; # echo $thumb; } } //isset $con = mysql_connect("localhost", "root") or die(mysql_error()); $db = mysql_select_db("delmar", $con); $name = $_POST['picname']; $pic = ($_FILES['myphoto']['name']); $date = date("m-d-y") . " at " . date("G:i:s"); $time = time(); if ($name && $pic) { $sql1 = mysql_query("INSERT INTO `mar_photo` (`name`,`photo`,`date`,`time`)VALUES ('$name','$pic','$date','$time')"); } else { echo "Error.............."; }//end if header("Location: myphotos.php"); ob_end_flush(); ?> thx runei Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 6, 2009 Share Posted March 6, 2009 What a coincidence- I was just reading this post http://www.phpfreaks.com/forums/index.php/topic,241827.msg1128334.html#msg1128334 There might be some useful pointers on there... Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 Hi Similar idea of dynamically changing the image size. I have a page setup that returns the users screen width / height in cookies, and then resizes the images to a best fit while keeping the ratio of width to height. I have put it below. I am sure you can chop this around to use a set max width / height (and probably clean the code up while you are at it). function imageResize($width, $height) { //takes the larger size of the width and height and applies the formula accordingly...this is so this script will work dynamically with any size image if (isset($_COOKIE["users_resolution_x"]) && isset($_COOKIE["users_resolution_y"]) ) { $screenSpaceX = $_COOKIE["users_resolution_x"] - 20; //If the Zeta logo is to be displayed under the image //$screenSpaceY = $_COOKIE["users_resolution_y"] - 67; $screenSpaceY = $_COOKIE["users_resolution_y"] - 20; if ($screenSpaceX > 200 && $screenSpaceY > 200) { $wPercentage = $screenSpaceX / $width; $hPercentage = $screenSpaceY / $height; if ($wPercentage > $hPercentage) { $percentage = $hPercentage; } else { $percentage = $wPercentage; } If ($percentage > 1 && $percentage < 1.5) { $percentage = 1; } switch (true) { case ($percentage > 1 && $percentage < 1.5) : $percentage = 1; break; case ($percentage >= 1.5 && $percentage < 2) : $percentage = 1.5; break; case ($percentage >= 2 && $percentage < 3) : $percentage = 2; break; case ($percentage >= 3 && $percentage < 5) : $percentage = 3; break; default : break; } //gets the new value and applies the percentage, then rounds the value $width = round($width * $percentage); $height = round($height * $percentage); } } //returns the new sizes in html image tag format...this is so you can plug this function inside an image tag and just get the return "width:".$width."px;height:".$height."px;"; } All the best Keith Quote Link to comment Share on other sites More sharing options...
runei Posted March 6, 2009 Author Share Posted March 6, 2009 Im still trying to figure it out. When implementing height the thumbnail goes black, after what i read this has something to do with the path. I know the path is correct so it is the script i am messing up. Btw, does imagesx and imagesy accomplish the same thing as getimagesize? Better to go with getimagesize then.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.