PHP - Php Thump Crop
Hi I want to build a script in php that take from user an image and split(crop) the image to 8 or 12 parts equal in the size for puzzel. my problem is how to crope the image to some number 8? I have a link for libery name phpthumb the adress is : http://phpthumb.gxdlabs.com/ In the libery there is a folder examples/crop_basic.php the file crop_basic.php do the missuen but only for one part is crop I dont know how to crop more then one Can you help me please thanks
Similar TutorialsHello everyone, In fact, I have a script that makes the crop image that works well, I'm stuck on the part of adding a text box to allow to name the image "thumb". I also ask if possible to remove the "PHP_SELF" script and divide it into several pages. Attached script There is a picture with dimensions of 10000 by 10000 pixels. Need to display a picture with dimensions of 200 by 100. Only need to use native php. I need to set the width and let height adjust to the proportions of width. Can someone please help me with this? I understand that $canvas_height should be 160, but then the image will crop to whichever side is greatest not by the width only. Code: [Select] <?php $img = "image.jpg"; $canvas_width = 160; $canvas_height = 0; list($img_width, $img_height) = getimagesize($img); $ratio_orig = $img_width / $img_height; if($canvas_width / $canvas_height > $ratio_orig) { $canvas_width = $canvas_height * $ratio_orig; } else { $canvas_height = $canvas_width / $ratio_orig; } $original = imagecreatefromjpeg($img); $canvas = imagecreatetruecolor($canvas_width, $canvas_height); imagecopyresampled($canvas, $original, 0, 0, 0, 0, $canvas_width, $canvas_height, $img_width, $img_height); $dest = "medium/image.jpg"; imagejpeg($canvas, $dest, 100); ?> I've been creating my own image upload script which takes in several parameters to style an image, however when the function is used new width and new height are specified but I need to use these values to determine cropping coordinates. What I want is when a new width and height is specified, if it is squared i.e. new width is equal to new height AND the original image is not squared i.e. it is a vertical or horizontal rectangle then the central part of that image will be taken. So for example if the user uploads an image 500px high and 100px wide and sets the new width and height both at 100px the new image should be a 100px by 100px square which is taken 200px from the top of the original image. Here is the functions I currently use so far. It's just the coordinates function I can't seem to figure out. Can anyone please help me out:- Code: [Select] //gets the extension of an uploaded file function getExtension($file) { $i = strrpos($file,".");//Gets the position of the "." in the filename if (!$i) { return ""; }//If there is no "." the function ends and returns "" $l = strlen($file) - $i; $ext = substr($file,$i+1,$l); $ext = strtolower($ext); return $ext; } //checks image dimensions and scales both sizes by ratio if it exceeds the max //$w = width, $mw = max width, $h = height, $mh = max height, function checkSize($w, $mw, $h, $mh){ if($w > $mw){//Checks if width is greater than max width $ratio = $mw / $w; $h = $h * $ratio; $h = round($h); $w = $mw; } if($h > $mh){//Checks if height is greater than max height $ratio = $mh / $h; $w = $w * $ratio; $w = round($w); $h = $mh; } return array($w, $h); } //Used to get the coordinates to resize an image by function getCoords(){ } //$f = the file, $ext = file extension, $nw = new width, $nh = new height, $mw = max width, $mh = max height, $nf = new filename, $fo = folder, $des = file size description, $q = quality function imageUpload($f, $ext, $nw, $nh, $mw, $mh, $nf, $fo, $des, $q){ //create image from uploaded file type if($ext=="jpg" || $ext=="jpeg" ){ $src = imagecreatefromjpeg($f); }else if($ext=="png"){ $src = imagecreatefrompng($f); }else{ $src = imagecreatefromgif($f); } //creates a list of the width and height of the image list($w,$h)=getimagesize($f); //sets the coordinates for resizing to 0 by default $dx = $dy = $sx = $sy = 0; //if new width and height are both 0 then a resize is not required so original dimensions need to be validated in case they exceed their max if($nw == 0 && $nh == 0){ if($w > $mw || $h > $mh){//checks if width or height are greater than their max list($w, $h) = checkSize($w, $mw, $h, $mh); } $nw = $w; $nh = $h; }else if($nw == $nh && $w !== $h){//this is for if the resized image needs to be squared but the original image is not a square //COORDS FUNCTION NEEDED HERE } $desext = "";//sets the description extension to "" by default if($des !== 0){//Checks if $des is set or not $desext .= "_".$des;//appends des to $desext ready to be appended to the filename } $foext = "";//sets the folder extension to "" by default if($fo !== 0){//Checks if $fo is set or not $foext .= $fo."/";//appends folder to $foext ready to be appended to the filename } $qv = 100;//sets the quality value to 100 percent by default if($q !== 0){//Checks if $q is set or not $qv .= $q;//sets the quality value to the passed value } $tmp=imagecreatetruecolor($nw,$nh); imagecopyresampled($tmp,$src,$dx,$dy,$sx,$sy,$nw,$nh,$w,$h); $fn = "images/".$foext.$nf.$desext.".jpg";//sets the final filename for upload imagejpeg($tmp,$fn,$qv);//uploads the file //empty variables and clear image imagedestroy($src); imagedestroy($tmp); } I'll explain with an example: I have this image: http://www.fitnessvital.com/images/galeria/caballo-pastando-0.jpg I create a thumbnail of it: http://www.fitnessvital.com/images/galeria/thumbs/caballo-pastando-0.jpg You can see that it's a big image where the important thing is the horse. The grass is not important. So, the best thing to do it's to crop the extra grass and resizing the rest. So the question is: There's a way of recognizing the objects of an image and crop the rest ? Or identify the non important of an imagen and cropt it ? (the same rewrited) Thank you i have this awesome script i've used on multiple websites and only now is it giving me trouble. it's always aligned/cropped to the middle of the image but for some reason the smaller thumbnails are cropping to the bottom. Code: [Select] <?php header ("Content-type: image/jpeg"); $file_name=$_GET['f']; $crop_height=$_GET['h']; $crop_width=$_GET['w']; $file_type= explode('.', $file_name); $file_type = $file_type[count($file_type) -1]; $file_type=strtolower($file_type); $original_image_size = getimagesize($file_name); $original_width = $original_image_size[0]; $original_height = $original_image_size[0]; if($file_type=='jpg') { $original_image_gd = imagecreatefromjpeg($file_name); } if($file_type=='gif') { $original_image_gd = imagecreatefromgif($file_name); } if($file_type=='png') { $original_image_gd = imagecreatefrompng($file_name); } $cropped_image_gd = imagecreatetruecolor($crop_width, $crop_height); $wm = $original_width /$crop_width; $hm = $original_height /$crop_height; $h_height = $crop_height/2; $w_height = $crop_width/2; if($original_width > $original_height ) { $adjusted_width =$original_width / $hm; $half_width = $adjusted_width / 2; $int_width = $half_width - $w_height; imagecopyresampled($cropped_image_gd ,$original_image_gd ,-$int_width,0,0,0, $adjusted_width, $crop_height, $original_width , $original_height ); } elseif(($original_width < $original_height ) || ($original_width == $original_height )) { $adjusted_height = $original_height / $wm; $half_height = $adjusted_height / 2; $int_height = $half_height - $h_height; imagecopyresampled($cropped_image_gd , $original_image_gd ,0,-$int_height,0,0, $crop_width, $adjusted_height, $original_width , $original_height ); } else { imagecopyresampled($cropped_image_gd , $original_image_gd ,0,0,0,0, $crop_width, $crop_height, $original_width , $original_height ); } imagejpeg($cropped_image_gd); ?> if i modify the value of original_image_size, it crops to the middle, but if the height is greater than the width it will add black on either side of the image to fill out the designated value. Code: [Select] $original_image_size = getimagesize($file_name); $original_width = $original_image_size[0]; $original_height = $original_image_size[1]; i can't figure out how to get it do both. 0, 0 outputs this: http://www.loudtechinc.com/images/scriptissue/00.png http://www.loudtechinc.com/images/scriptissue/00n2.png 0, 1 outputs this: http://www.loudtechinc.com/images/scriptissue/01.png http://www.loudtechinc.com/images/scriptissue/01n2.png The end goal is to have it fill the desired height and width while cropping to the middle. I want to reduce a picture size from 600px * 500px to 60px * 50px size, then crop it become 50px *50px. I have two groups of codes, 1 is to reduce the size of image, other 1 is to crop the image. The problem is they works separately, how to combine this two groups of codes to make them work together? Below is my codes : Code: [Select] <?php //codes of group A - Reduce the size of image from 600px * 500px to 60px * 50px $save2 = "images/users/" . $image_name_2; //This is the new file you saving list($width2, $height2) = getimagesize($file) ; $modwidth2 = 50; $diff2 = $width2 / $modwidth2; $modheight2 = $height2 / $diff2; $tn2 = imagecreatetruecolor($modwidth2, $modheight2) ; $image2 = imagecreatefromjpeg($file) ; imagecopyresampled($tn2, $image2, 0, 0, 0, 0, $modwidth2, $modheight2, $width2, $height2) ; imagejpeg($tn2, $save2, 100) ; //codes of group B - Crop the image from 60px * 50px to 50px * 50px $save3 = "images/users/" . $image_name_3; list($width3, $height3) = getimagesize($file) ; $modwidth3 = 60; $diff3 = $width3 / $modwidth3; $modheight3 = $height3 / $diff3; $left = 0; $top = 0; $cropwidth = 50; //thumb size $cropheight = 50; $tn3 = imagecreatetruecolor($cropwidth, $cropheight) ; $image3 = imagecreatefromjpeg($file) ; imagecopyresampled($tn3, $image3, 0, 0, $left, $top, $cropwidth, $cropheight, $modwidth3, $modheight3) ; imagejpeg($tn3, $save3, 100) ; //save the cropped image ?> As you can see from 2 groups of codes above, 1st group resize the pic then save it to a folder. 2nd group of codes crop the pic then save it into the folder too. My question is ... After 1st group of codes resize the picture, is it necessary to save it into folder before I can crop it? If it is necessary, then I need to write new lines of codes to retrieve the resized pic from the folder for 2nd group of codes to crop it? If it is not necessary, after resizing the pic, how do I pass the pic to 2nd group of codes to crop it? php image-resizing |