Lodius2000 Posted July 7, 2008 Share Posted July 7, 2008 alrighty, I have a working image resizer and uploader, what i would like is for the script to compare the dimensions of the photo and determine which is longer, then make the resize on that dimension so that a skinny but tall photo is shortened up and a fat short photo is thinned up. the script makes 2 files a big and small file here is the relvant code <?php sm_pixels = 250; bg_pixels = 512; //determine which is longer, height or width if ($width >= $height) { //big $new_bg_height=($height_orig/$width_orig)*$bg_pixels; $tmp_bg=imagecreatetruecolor($bg_width,$new_bg_height); //small $new_sm_height=($height_orig/$width_orig)*$sm_pixels; $tmp_sm=imagecreatetruecolor($sm_width,$new_sm_height); // these lines actually does the image resizing, copying from the original // image into the $tmp image imagecopyresampled($tmp_bg,$src_bg,0,0,0,0,$bg_width,$new_bg_height,$width_orig,$height_orig); imagecopyresampled($tmp_sm,$src_sm,0,0,0,0,$sm_width,$new_sm_height,$width_orig,$height_orig); } else { //big $new_bg_width=($width_orig/$height_orig)*$bg_pixels; $tmp_bg=imagecreatetruecolor($new_bg_width,$bg_height); //small $new_sm_width=($width_orig/$height_orig)*$sm_pixels; $tmp_sm=imagecreatetruecolor($new_bg_width,$sm_height); imagecopyresampled($tmp_bg,$src_bg,0,0,0,0,$bg_width,$new_bg_height,$width_orig,$height_orig); imagecopyresampled($tmp_sm,$src_sm,0,0,0,0,$sm_width,$new_sm_height,$width_orig,$height_orig); } ?> so the script always makes the width 512 for big or 250 for small, i want it to choose which is longer and resize that dimension to 512 or 250, so that i don't end up with a hugely long or hugely wide photo on my website what did i do wrong thanks Link to comment https://forums.phpfreaks.com/topic/113508-image-resize-and-horizontal-vs-vertical/ Share on other sites More sharing options...
john-formby Posted July 7, 2008 Share Posted July 7, 2008 Hi, I have a script that I use for uploading photos to my site that does exactly what you want. It resizes correctly (width & height) and creates 2 files (big & small). You can specify the size of the file and the location to save. Have a look and let me know if you have any problem. <?php include('common/dbconnect.php'); if(isset($_POST['submit'])) { //rename file storage variable for convenience $img = $_FILES['uploadfile']; //get the type of the file //note that the type can be determined by the MIME type, but this is prone to error and can be fooled. //This way, even if the user is uploading an executable, if it is named .jpg, it can never be executed //the type is found by taking the substring of the name after the last '.', strrpos finds the last instance of a character in a string $type = substr($img['name'], strrpos($img['name'], '.') + 1); //ensure the type is gif or jpg, and that the size is less than 512kB (expressed in bytes) if(($type == "gif" || $type == "jpg") && $img['size'] < 512000) { //get the current timestamp $time = time(); //create the names for the two files //first, get the name of the file, using a similar method to that getting the type earlier $name = substr($img['name'], 0, strrpos($img['name'], '.')); //then it's just a matter of putting the pieces together $fullsizeName = $name.'_'.$time.'.'.$type; $thumbName = $name.'_'.$time.'_thumb.'.$type; //create a php image object from the uploaded file if($type == "gif") $imgObj = imagecreatefromgif($img['tmp_name']); else $imgObj = imagecreatefromjpeg($img['tmp_name']); //get the width and height of the image $width = imageSX($imgObj); $height = imageSY($imgObj); //resize both width and height if width > 600 if($width > 600) { $height = $height * (600 / $width); $width = 600; } //get the thumb width and height: if($height * 1.5 < $width) { //if so, we limit by width $thumbWidth = 150; $thumbHeight = $height * (150 / $width); } else { //otherwise, we limit by height $thumbHeight = 100; $thumbWidth = $width * (100 / $height); } //create the new image objects $newImage = imagecreatetruecolor($width, $height); $newThumb = imagecreatetruecolor($thumbWidth, $thumbHeight); //copy the old image objects to the new ones, given their new dimensions //see further documentation at php.net/imagecopyresampled imagecopyresampled($newImage, $imgObj, 0, 0, 0, 0, $width, $height, imageSX($imgObj), imageSY($imgObj)); imagecopyresampled($newThumb, $imgObj, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imageSX($imgObj), imageSY($imgObj)); //move the images to their destinations if($type == "gif") { imagegif($newImage, 'images/photo/'.$fullsizeName); imagegif($newThumb, 'images/photo/'.$thumbName); } else { imagejpeg($newImage, 'images/photo/'.$fullsizeName); imagejpeg($newThumb, 'images/photo/'.$thumbName); } //destroy the image objects to save space on the server imagedestroy($imgObj); imagedestroy($newImage); imagedestroy($newThumb); //insert data into the database - ID (autonumber), mysql_query("INSERT INTO tblgallery VALUES ('', '".$fullsizeName."', '".$thumbName."');"); } else echo "The input is not acceptable"; } echo '<form name="uploadimage" action="upload.php" method="post"> <input class="input6" type="file" name="uploadfile" /> <input type="submit" name="submit" value="submit" /> </form>'; ?> Link to comment https://forums.phpfreaks.com/topic/113508-image-resize-and-horizontal-vs-vertical/#findComment-583356 Share on other sites More sharing options...
Lodius2000 Posted July 9, 2008 Author Share Posted July 9, 2008 Thanks john but I am trying to do all my own original code for resume's sake and that is no all I have this script do, and it does work, just not all the features work as I would like, soo.... bump Link to comment https://forums.phpfreaks.com/topic/113508-image-resize-and-horizontal-vs-vertical/#findComment-585870 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.