jen91 Posted March 17, 2009 Share Posted March 17, 2009 hey guys, i am new here and new to php i am trying to create a thumbnail image from part of a larger image when it is uploaded. i found some code that takes the large image and creates a thumbnail. this is a proportional thumbnail though. what i want to be able to do is have it create a thumbnail that is 288 x 125 pixels by just taking the top corner of the large image. i don't want to shrink it as it gets all distorted. i don't even know if this is possible or if what i typed makes any sense to anyone i couldn't find anything in my searches about this anyways here is the code i am using at the moment $triplast1 = mysql_insert_id(); $path = '../images/photos/'.$triplast1.'/'; mkdir($path,0777); chmod($path,0777); $img = $_FILES['uploadfile']; $type = substr($img['name'], strrpos($img['name'], '.') + 1); if(($type == "gif" || $type == "jpg") && $img['size'] < 1024000) { $time = time(); $name = substr($img['name'], 0, strrpos($img['name'], '.')); $fullsizeName = $name.'_'.$time.'.'.$type; $thumbName = $name.'_'.$time.'_thumb.'.$type; if($type == "gif") $imgObj = imagecreatefromgif($img['tmp_name']); else $imgObj = imagecreatefromjpeg($img['tmp_name']); $width = imageSX($imgObj); $height = imageSY($imgObj); if($width > 720) { $height = $height * (720 / $width); $width = 720; } if($height * 1.5 < $width) { $thumbWidth = 288; $thumbHeight = $height * (288 / $width); } else { $thumbHeight = 125; $thumbWidth = $width * (125 / $height); } $newImage = imagecreatetruecolor($width, $height); $newThumb = imagecreatetruecolor($thumbWidth, $thumbHeight); 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)); if($type == "gif") { imagegif($newImage, '../images/photos/'.$triplast1.'/'.$fullsizeName); imagegif($newThumb, '../images/photos/'.$triplast1.'/'.$thumbName); } else { imagejpeg($newImage, '../images/photos/'.$triplast1.'/'.$fullsizeName); imagejpeg($newThumb, '../images/photos/'.$triplast1.'/'.$thumbName); } imagedestroy($imgObj); imagedestroy($newImage); imagedestroy($newThumb); } $query = "UPDATE tbltrips SET tmaptmb = '$thumbName', tmaplrg = '$fullsizeName' WHERE tID = '$triplast1'"; $result = mysql_query($query); thanks Link to comment https://forums.phpfreaks.com/topic/149822-thumbnail-part-of-larger-image/ Share on other sites More sharing options...
jen91 Posted March 17, 2009 Author Share Posted March 17, 2009 bump Link to comment https://forums.phpfreaks.com/topic/149822-thumbnail-part-of-larger-image/#findComment-787161 Share on other sites More sharing options...
samshel Posted March 17, 2009 Share Posted March 17, 2009 u mean u just need the 288 x 125 pixels of the top corner and not resize the image actually? if yes, then what you are looking for is cropping and not resizing. check imagecopyresampled() Link to comment https://forums.phpfreaks.com/topic/149822-thumbnail-part-of-larger-image/#findComment-787165 Share on other sites More sharing options...
jen91 Posted March 17, 2009 Author Share Posted March 17, 2009 oh yeh, image cropping. couldn't remember what it was called i will have a look at that Link to comment https://forums.phpfreaks.com/topic/149822-thumbnail-part-of-larger-image/#findComment-787189 Share on other sites More sharing options...
jen91 Posted March 19, 2009 Author Share Posted March 19, 2009 ahhhh i need help i have been trying to get this cropping thing working with the existing code but i am struggling i am using this code $path = '../images/photos/'.$triplast1.'/'; mkdir($path,0777); chmod($path,0777); $img = $_FILES['uploadfile']; $type = substr($img['name'], strrpos($img['name'], '.') + 1); if(($type == "gif" || $type == "jpg") && $img['size'] < 1024000) { $time = time(); $name = substr($img['name'], 0, strrpos($img['name'], '.')); $fullsizeName = $name.'_'.$time.'.'.$type; $thumbName = $name.'_'.$time.'_thumb.'.$type; if($type == "gif") $imgObj = imagecreatefromgif($img['tmp_name']); else $imgObj = imagecreatefromjpeg($img['tmp_name']); $width = imageSX($imgObj); $height = imageSY($imgObj); if($width > 720) { $height = $height * (720 / $width); $width = 720; } $thumbw = 288; $thumbh = 125; $newImage = imagecreatetruecolor($width, $height); $newThumb = imagecreatetruecolor($thumbw, $thumbh); $wm = $width/$thumbw; $hm = $height/$thumbh; $h_height = $thumbh/2; $w_height = $thumbw/2; imagecopyresampled($newImage, $imgObj, 0, 0, 0, 0, $width, $height, imageSX($imgObj), imageSY($imgObj)); $adjusted_width = $width / $hm; $half_width = $adjusted_width / 2; $int_width = $half_width - $w_height; imagecopyresampled($newThumb, $imgObj, -$int_height, 0, 0, 0, $adjusted_width, $thumbh, $width, $height); if($type == "gif") { imagegif($newImage, '../images/photos/'.$triplast1.'/'.$fullsizeName); imagegif($newThumb, '../images/photos/'.$triplast1.'/'.$thumbName,100); } else { imagejpeg($newImage, '../images/photos/'.$triplast1.'/'.$fullsizeName); imagejpeg($newThumb, '../images/photos/'.$triplast1.'/'.$thumbName,100); } imagedestroy($imgObj); imagedestroy($newImage); imagedestroy($newThumb); } $query = "UPDATE tbltrips SET tmaptmb = '$thumbName', tmaplrg = '$fullsizeName' WHERE tID = '$triplast1'"; $result = mysql_query($query); it is creating the folder, larger image and thumbnail which is fine. the problem is that the thumnail is not cropping, is is just scaling so i end up with a small image and the rest of it is just black background. i have tried a few different things but i cant get it to work please please please can somebody help me Link to comment https://forums.phpfreaks.com/topic/149822-thumbnail-part-of-larger-image/#findComment-788358 Share on other sites More sharing options...
jen91 Posted March 19, 2009 Author Share Posted March 19, 2009 is there anybody that can help me with this? Link to comment https://forums.phpfreaks.com/topic/149822-thumbnail-part-of-larger-image/#findComment-788981 Share on other sites More sharing options...
samshel Posted March 20, 2009 Share Posted March 20, 2009 $width = imageSX($imgObj); $height = imageSY($imgObj); ..... imagecopyresampled($newImage, $imgObj, 0, 0, 0, 0, $width, $height, imageSX($imgObj), imageSY($imgObj)); i think $width is same as imageSX($imgObj) and $height is same as imageSY($imgObj). so ur passing the destination height and width the same as source, so it will give u image of the same size, it crops the image but the size is same so it fills remaining area with black color.. please carefully study each parameter of the function imagecopyresampled and check if you are supplying them correctly. Link to comment https://forums.phpfreaks.com/topic/149822-thumbnail-part-of-larger-image/#findComment-789019 Share on other sites More sharing options...
jen91 Posted March 20, 2009 Author Share Posted March 20, 2009 solved it changed it to imagecopyresampled($newThumb, $imgObj, 0, 0, 0, 0, $thumbw, $thumbh, $thumbw, $thumbh); and it works Link to comment https://forums.phpfreaks.com/topic/149822-thumbnail-part-of-larger-image/#findComment-789262 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.