theCro Posted January 16, 2008 Share Posted January 16, 2008 Hello everyone i've been using this php code to resize image 'on the fly' and it really rocks! here's the code: <?php header ("Content-type: image/jpeg"); /* JPEG / PNG Image Resizer Parameters (passed via URL): img = path / url of jpeg or png image file percent = if this is defined, image is resized by it's value in percent (i.e. 50 to divide by 50 percent) w = image width h = image height constrain = if this is parameter is passed and w and h are set to a size value then the size of the resulting image is constrained by whichever dimension is smaller Requires the PHP GD Extension Outputs the resulting image in JPEG Format By: Michael John G. Lopez - http://www.sydel.net Filename : imgsize.php */ $img = $_GET['img']; $percent = $_GET['percent']; $constrain = $_GET['constrain']; $w = $_GET['w']; $h = $_GET['h']; // get image size of img $x = @getimagesize($img); // image width $sw = $x[0]; // image height $sh = $x[1]; if ($percent > 0) { // calculate resized height and width if percent is defined $percent = $percent * 0.01; $w = $sw * $percent; $h = $sh * $percent; } else { if (isset ($w) AND !isset ($h)) { // autocompute height if only width is set $h = (100 / ($sw / $w)) * .01; $h = @round ($sh * $h); } elseif (isset ($h) AND !isset ($w)) { // autocompute width if only height is set $w = (100 / ($sh / $h)) * .01; $w = @round ($sw * $w); } elseif (isset ($h) AND isset ($w) AND isset ($constrain)) { // get the smaller resulting image dimension if both height // and width are set and $constrain is also set $hx = (100 / ($sw / $w)) * .01; $hx = @round ($sh * $hx); $wx = (100 / ($sh / $h)) * .01; $wx = @round ($sw * $wx); if ($hx < $h) { $h = (100 / ($sw / $w)) * .01; $h = @round ($sh * $h); } else { $w = (100 / ($sh / $h)) * .01; $w = @round ($sw * $w); } } } $im = @ImageCreateFromJPEG ($img) or // Read JPEG Image $im = @ImageCreateFromPNG ($img) or // or PNG Image $im = @ImageCreateFromGIF ($img) or // or GIF Image $im = false; // If image is not JPEG, PNG, or GIF if (!$im) { // We get errors from PHP's ImageCreate functions... // So let's echo back the contents of the actual image. readfile ($img); } else { // Create the resized image destination $thumb = @ImageCreateTrueColor ($w, $h); // Copy from image source, resize it, and paste to image destination @ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh); // Output resized image @ImageJPEG ($thumb); } ?> [sAVE FILE AS 'imgsize.php'] BUT, i wanted to add new function so when, for example if the image is 600x500 and i want it to be 300x120 ... now it crops and STRETCHES the image and it's unproportional.. what i want is to make it proportional even if it's needed to cut the edges to fit the 300x120 pixels size... hopefully you understood what i wanted.. anyway the code for view image is http://www.yourwebsite.com/imgsize.php?w=200&h=100&img=foto/enghrv2.jpg i know i have to use http://bg2.php.net/imagecopy to crop the image as desired. but i do not know how to import it into my above posted code someone please help, it's for my kid's soccer website, thanks alot! cheers! Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/ Share on other sites More sharing options...
dooper3 Posted January 16, 2008 Share Posted January 16, 2008 Are you sure you mean you want to crop the image, or do you mean you want to resize it? They are different things. Cropping actually removes part of the image, but the rest of it stays the same size as it was before (though the overall image becomes smaller as a result of some of it being removed). Resizing is just taking an image and making it smaller, but keeping all of the original picture. Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-440706 Share on other sites More sharing options...
theCro Posted January 16, 2008 Author Share Posted January 16, 2008 Are you sure you mean you want to crop the image, or do you mean you want to resize it? They are different things. Cropping actually removes part of the image, but the rest of it stays the same size as it was before (though the overall image becomes smaller as a result of some of it being removed). Resizing is just taking an image and making it smaller, but keeping all of the original picture. thanks for your time and your answer here is real live example how i want it to work: http://www.index.hr/thumbnail.ashx?o=c&w=220&h=100&path=images2/BracaMamicInx.jpg change w=220 and h=100 hopefully youcan fill my code with code i need thanks man! Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-440861 Share on other sites More sharing options...
theCro Posted January 16, 2008 Author Share Posted January 16, 2008 for example on the link i posted above if you change w= and h= it will change picture size right away to desired size and will crop the rest and again if you change the size, image stays the same.. try to look at http://www.index.hr/thumbnail.ashx?o=c&w=520&h=200&path=images2/BracaMamicInx.jpg and change to http://www.index.hr/thumbnail.ashx?o=c&w=520&h=300&path=images2/BracaMamicInx.jpg and you'll see what am i talking about... waiting for your respond. thank you for your time! Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441083 Share on other sites More sharing options...
theCro Posted January 16, 2008 Author Share Posted January 16, 2008 anyone can help me? it's for my son's soccer team web site come one . anyone! Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441339 Share on other sites More sharing options...
interpim Posted January 16, 2008 Share Posted January 16, 2008 You might try this script here http://mondaybynoon.com/2007/12/17/crop-and-resize-images-with-gd-or-imagemagick-v11/ Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441343 Share on other sites More sharing options...
theCro Posted January 16, 2008 Author Share Posted January 16, 2008 You might try this script here http://mondaybynoon.com/2007/12/17/crop-and-resize-images-with-gd-or-imagemagick-v11/ just look at http://www.index.hr/thumbnail.ashx?o=c&w=220&h=100&path=images2/ivankoviccibalia-v.jpg and see what exactly i need and change w= and h= it's all on the fly... Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441380 Share on other sites More sharing options...
theCro Posted January 17, 2008 Author Share Posted January 17, 2008 ok i managed to modify my code now it looks like this: @ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $w, $h); however, i'd like that it centers image then cut the edges ... please help! Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441551 Share on other sites More sharing options...
theCro Posted January 17, 2008 Author Share Posted January 17, 2008 sorry. the code i posted @ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh); only crop edges as desired but does not resize the image.. now i'm in total mess someone give me a hand please Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441557 Share on other sites More sharing options...
theCro Posted January 17, 2008 Author Share Posted January 17, 2008 ok it seems like it's all about ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh); code the problem is i dont know how t make it work with the code i posed on my first post (at the top of the page) someone please solve this for me, i've been trying 4 days all day night long and came up with what i have now ... please someone help me here! Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441614 Share on other sites More sharing options...
priti Posted January 17, 2008 Share Posted January 17, 2008 Hi, I was working with your script and was able to see correct output .Just paste this script in some other file and see weather it works or not.I have not chnaged much but still give a try <?php /* JPEG / PNG Image Resizer Parameters (passed via URL): img = path / url of jpeg or png image file percent = if this is defined, image is resized by it's value in percent (i.e. 50 to divide by 50 percent) w = image width h = image height constrain = if this is parameter is passed and w and h are set to a size value then the size of the resulting image is constrained by whichever dimension is smaller Requires the PHP GD Extension Outputs the resulting image in JPEG Format By: Michael John G. Lopez - http://www.sydel.net Filename : imgsize.php */ //$img='C:\wamp\www\test\thumbnail.jpg'; $img=$_GET['img']; $percent = $_GET['percent']; $constrain = $_GET['constrain']; $w = $_GET['w']; $h = $_GET['h']; // get image size of img $x = getimagesize($img); // image width $sw = $x[0]; // image height $sh = $x[1]; if ($percent > 0) { // calculate resized height and width if percent is defined $percent = $percent * 0.01; $w = $sw * $percent; $h = $sh * $percent; } else { if (isset ($w) AND !isset ($h)) { // autocompute height if only width is set $h = (100 / ($sw / $w)) * .01; $h = @round ($sh * $h); } elseif (isset ($h) AND !isset ($w)) { // autocompute width if only height is set $w = (100 / ($sh / $h)) * .01; $w = @round ($sw * $w); } elseif (isset ($h) AND isset ($w) AND isset ($constrain)) { // get the smaller resulting image dimension if both height // and width are set and $constrain is also set $hx = (100 / ($sw / $w)) * .01; $hx = @round ($sh * $hx); $wx = (100 / ($sh / $h)) * .01; $wx = @round ($sw * $wx); if ($hx < $h) { $h = (100 / ($sw / $w)) * .01; $h = @round ($sh * $h); } else { $w = (100 / ($sh / $h)) * .01; $w = @round ($sw * $w); } } } $im = @ImageCreateFromJPEG ($img); if (!$im) { // We get errors from PHP's ImageCreate functions... // So let's echo back the contents of the actual image. readfile ($img); } else { // Create the resized image destination $thumb = ImageCreateTrueColor ($w, $h); // Copy from image source, resize it, and paste to image destination ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh); // Output resized image header ("Content-type: image/jpeg"); ImageJPEG ($thumb); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441642 Share on other sites More sharing options...
cooldude832 Posted January 17, 2008 Share Posted January 17, 2008 this is what I made (no percentage based only a set width/height <?php img_crop_size($_GET['img'],$_GET['w'],$_GET['h']); function img_crop_size($img,$width,$height){ #varify that all values are valid (between 10-2500) if(intval($width) < 10 || intval($height) <10 || intval($width) > 2500 || intval($height) > 2500){ return "Error"; } #make sure its a valid image before wasting resources elseif(!is_array(getimagesize($img))){ return "Error"; } #its an image and we have valid arguements for height.width else{ $thumb = imagecreatetruecolor($width, $height); $imgdata = getimagesize($img); //print_r($imgdata); switch ($imgdata['mime']){ case "image/jpeg": header('Content-type: image/jpg'); $op ="imagejpeg"; $source = imagecreatefromjpeg($img); break; case "image/gif": header('Content-type: image/gif'); $op ="imagegif"; $source = imagecreatefromgif($img); break; case "image/png": header('Content-type: image/png'); $op = "imagepng"; $source = imagecreatefrompng($img); break; } if(ISSET($source){ imagecopyresized($thumb, $source,0,0,0,0,$imgdata[0],$imgdata[1],$imgdata[0],$imgdata[1]); if($imgdata[0] > $width){ //The original image is greater in the X direction than the new so we must shrink it $o_width = $width; } else{ $o_width = $imgdata[0]; } if($imgdata[1] > $height){ //The original image is greater in the y direction than the new so we must shrink it $o_height = $height; } else{ $o_height = $imgdata[1]; } imagecopyresized($thumb, $source,0,0,0,0,$width,$height,$o_width,$o_height); $op($thumb, null, 100); imagedestroy($thumb); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441648 Share on other sites More sharing options...
theCro Posted January 17, 2008 Author Share Posted January 17, 2008 Hi, I was working with your script and was able to see correct output .Just paste this script in some other file and see weather it works or not.I have not chnaged much but still give a try <?php /* JPEG / PNG Image Resizer Parameters (passed via URL): img = path / url of jpeg or png image file percent = if this is defined, image is resized by it's value in percent (i.e. 50 to divide by 50 percent) w = image width h = image height constrain = if this is parameter is passed and w and h are set to a size value then the size of the resulting image is constrained by whichever dimension is smaller Requires the PHP GD Extension Outputs the resulting image in JPEG Format By: Michael John G. Lopez - http://www.sydel.net Filename : imgsize.php */ //$img='C:\wamp\www\test\thumbnail.jpg'; $img=$_GET['img']; $percent = $_GET['percent']; $constrain = $_GET['constrain']; $w = $_GET['w']; $h = $_GET['h']; // get image size of img $x = getimagesize($img); // image width $sw = $x[0]; // image height $sh = $x[1]; if ($percent > 0) { // calculate resized height and width if percent is defined $percent = $percent * 0.01; $w = $sw * $percent; $h = $sh * $percent; } else { if (isset ($w) AND !isset ($h)) { // autocompute height if only width is set $h = (100 / ($sw / $w)) * .01; $h = @round ($sh * $h); } elseif (isset ($h) AND !isset ($w)) { // autocompute width if only height is set $w = (100 / ($sh / $h)) * .01; $w = @round ($sw * $w); } elseif (isset ($h) AND isset ($w) AND isset ($constrain)) { // get the smaller resulting image dimension if both height // and width are set and $constrain is also set $hx = (100 / ($sw / $w)) * .01; $hx = @round ($sh * $hx); $wx = (100 / ($sh / $h)) * .01; $wx = @round ($sw * $wx); if ($hx < $h) { $h = (100 / ($sw / $w)) * .01; $h = @round ($sh * $h); } else { $w = (100 / ($sh / $h)) * .01; $w = @round ($sw * $w); } } } $im = @ImageCreateFromJPEG ($img); if (!$im) { // We get errors from PHP's ImageCreate functions... // So let's echo back the contents of the actual image. readfile ($img); } else { // Create the resized image destination $thumb = ImageCreateTrueColor ($w, $h); // Copy from image source, resize it, and paste to image destination ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh); // Output resized image header ("Content-type: image/jpeg"); ImageJPEG ($thumb); } ?> its still stretched image... Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441659 Share on other sites More sharing options...
priti Posted January 17, 2008 Share Posted January 17, 2008 what problem i was able see in your url was image was getting chopped off and shown to you. where as with that script the whole image was getting resized with out any cropping. Regarding strected if you will try to give w,h greator then original only then it sud strected its size not in other case. Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441661 Share on other sites More sharing options...
theCro Posted January 17, 2008 Author Share Posted January 17, 2008 this is what I made (no percentage based only a set width/height <?php img_crop_size($_GET['img'],$_GET['w'],$_GET['h']); function img_crop_size($img,$width,$height){ #varify that all values are valid (between 10-2500) if(intval($width) < 10 || intval($height) <10 || intval($width) > 2500 || intval($height) > 2500){ return "Error"; } #make sure its a valid image before wasting resources elseif(!is_array(getimagesize($img))){ return "Error"; } #its an image and we have valid arguements for height.width else{ $thumb = imagecreatetruecolor($width, $height); $imgdata = getimagesize($img); //print_r($imgdata); switch ($imgdata['mime']){ case "image/jpeg": header('Content-type: image/jpg'); $op ="imagejpeg"; $source = imagecreatefromjpeg($img); break; case "image/gif": header('Content-type: image/gif'); $op ="imagegif"; $source = imagecreatefromgif($img); break; case "image/png": header('Content-type: image/png'); $op = "imagepng"; $source = imagecreatefrompng($img); break; } if(ISSET($source){ imagecopyresized($thumb, $source,0,0,0,0,$imgdata[0],$imgdata[1],$imgdata[0],$imgdata[1]); if($imgdata[0] > $width){ //The original image is greater in the X direction than the new so we must shrink it $o_width = $width; } else{ $o_width = $imgdata[0]; } if($imgdata[1] > $height){ //The original image is greater in the y direction than the new so we must shrink it $o_height = $height; } else{ $o_height = $imgdata[1]; } imagecopyresized($thumb, $source,0,0,0,0,$width,$height,$o_width,$o_height); $op($thumb, null, 100); imagedestroy($thumb); } } } ?> thank you for you beautiful code.. it actually crops it the good way, the only thing i now need is to resize it proportionally to weight size, and then crop the height (ALL CENTERED FOCUS) as like on http://www.index.hr/thumbnail.ashx?o=c&w=250&h=450&path=images2/ivankoviccibalia-v.jpg it zoomed the center... and original image is 600x300 (http://www.index.hr/thumbnail.ashx?o=c&w=600&h=300&path=images2/ivankoviccibalia-v.jpg) thanks and hopefully we will finish it! and make good script of it! thanks ! Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441662 Share on other sites More sharing options...
theCro Posted January 17, 2008 Author Share Posted January 17, 2008 what problem i was able see in your url was image was getting chopped off and shown to you. where as with that script the whole image was getting resized with out any cropping. Regarding strected if you will try to give w,h greator then original only then it sud strected its size not in other case. http://www.index.hr/thumbnail.ashx?o=c&w=250&h=450&path=images2/ivankoviccibalia-v.jpg this is resized original is http://www.index.hr/thumbnail.ashx?o=c&w=600&h=300&path=images2/ivankoviccibalia-v.jpg this zoomed the center and cropped everything else outside 250x450.. this is what i need thanks man for your time! i havent sleep 20 hours ,.. now i go to sleep hopefully when i wake up someone will solve my mystery thanks to everyone! Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441663 Share on other sites More sharing options...
theCro Posted January 17, 2008 Author Share Posted January 17, 2008 still nothing here all i get is cropped part starting from left top zero in weight and height... help Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-441889 Share on other sites More sharing options...
theCro Posted January 17, 2008 Author Share Posted January 17, 2008 <?php img_crop_size($_GET['img'],$_GET['w'],$_GET['h']); function img_crop_size($img,$width,$height){ #varify that all values are valid (between 10-2500) if(intval($width) < 10 || intval($height) <10 || intval($width) > 2500 || intval($height) > 2500){ return "Error"; } #make sure its a valid image before wasting resources elseif(!is_array(getimagesize($img))){ return "Error"; } #its an image and we have valid arguements for height.width else{ $thumb = imagecreatetruecolor($width, $height); $imgdata = getimagesize($img); //print_r($imgdata); switch ($imgdata['mime']){ case "image/jpeg": header('Content-type: image/jpg'); $op ="imagejpeg"; $source = imagecreatefromjpeg($img); break; case "image/gif": header('Content-type: image/gif'); $op ="imagegif"; $source = imagecreatefromgif($img); break; case "image/png": header('Content-type: image/png'); $op = "imagepng"; $source = imagecreatefrompng($img); break; } if(ISSET($source){ imagecopyresized($thumb, $source,0,0,0,0,$imgdata[0],$imgdata[1],$imgdata[0],$imgdata[1]); if($imgdata[0] > $width){ //The original image is greater in the X direction than the new so we must shrink it $o_width = $width; } else{ $o_width = $imgdata[0]; } if($imgdata[1] > $height){ //The original image is greater in the y direction than the new so we must shrink it $o_height = $height; } else{ $o_height = $imgdata[1]; } imagecopyresized($thumb, $source,0,0,0,0,$width,$height,$o_width,$o_height); $op($thumb, null, 100); imagedestroy($thumb); } } } ?> can anyone try to modify this code so if original image is 600x300 and you resize it to 400x150 that it actually resizes it to weight=400 and CUT BOTTOM to fit height=150 .. of course with aspect ratio when resizing image.. .thanks alot please anyone! Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-442159 Share on other sites More sharing options...
theCro Posted January 18, 2008 Author Share Posted January 18, 2008 come one please someone help... Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-442811 Share on other sites More sharing options...
harley1387 Posted January 18, 2008 Share Posted January 18, 2008 If you have ImageMagick installed on your server, see below. Read the comments you'll need to customize $server_path, $img_path, gravity (for cropping), and the path to ImageMagick. <?PHP //CURRENT IMG NAME; DESIRED IMG NAME (OPTIONAL); RESIZE WIDTH (OPTIONAL); RESIZE HEIGHT (OPTIONAL); DESIRED IMG FORMAT (jpg, png, etc.; OPTIONAL); CROP (yes; OPTIONAL) //IF CROPPING IS NOT SET TO yes, WILL RESIZE PROPORTIONALLY TO FIT USING THE WIDTH AND HEIGHT AS MAXIMUMS (IF PROVIDED) //DESIRED IMG NAME WILL OVERRIDE THE DESIRED IMG FORMAT e.g., original.jpg > new.jpg with format set to png WILL CREATE A JPG FILE CALLED new.jpg function convert_img ($img_start, $img_end, $width, $height, $format, $crop) { //FULL SERVER PATH TO THIS FILE -- REPLACE WITH YOUR INFO (ENTIRE PATH MAY DIFFER) $server_path = "/hsphere/local/home/www.yourdomain.com/"; //PATH TO IMAGES DIRECTORY $img_path = "images/"; //CHECK IF ORIGINAL FILE EXISTS if (is_file($server_path.$img_path.$img_start)) { //CHECK IF DESIRED IMG NAME IS SPECIFIED if (!$img_end){ //OTHERWISE CHECK IF FORMAT IS SPECIFIED - FORMAT WILL ONLY CHANGE IF IMG_END IS NOT SPECIFIED if ($format){ $img_arr = explode(".", $img_start); $img_end = $img_arr[0].".".$format; } else { $img_end = $img_start; } } //CHECK IF RESIZE WIDTH & RESIZE HEIGHT WERE PROVIDED; ADJUST ACCORDINGLY if (!$width && !$height){ $geometry = ""; } else { if (!$height) { $geometry = "-geometry ".$width; } else if (!$width) { $geometry = "-geometry X".$height; } else { $geometry = "-geometry ".$width."X".$height; } } //CHECK IF CROPPING IS REQUESTED - REQUIRES HEIGHT AND WIDTH TO BE SPECIFIED if ($crop == "yes"){ if (!$width || !$height){ return $error = "ERROR: Specify a width and height or set crop to 'no'"; } $img_size = getimagesize($img_path.$img_start); //CALCULATES ASPECT RATIO TO DETERMINE HOW TO RESIZE THE IMAGE PROPORTIONALLY TO MINIMIZE CROPPING if(($img_size[0]/$img_size[1]) >= ($width/$height)) { $geometry = "-geometry X".$height; } else { $geometry = "-geometry ".$width; } //CURRENTLY CROPS FROM THE CENTER OF THE IMAGE -- TRIMMING EQUAL AMOUNTS (ONLY IF NECESSARY) ON THE TOP/BOTTOM; LEFT/RIGHT //CROPPING OPTIONS FOR THE GRAVITY INCLUDE: NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast (CHANGE IF NECESSARY) $img_crop = "-gravity Center -crop ".$width."X".$height."+0+0"; } else { $img_crop = ""; } //CONVERT THE IMAGE //PATH TO IMAGEMAGICK: /usr/X11R6/bin/convert (MAY BE DIFFERENT ON YOUR SERVER - REPLACE IF NECESSARY) $tmpMakeIMG = "/usr/X11R6/bin/convert -density 300X300 -quality 100 $geometry $img_crop -colorspace RGB $server_path$img_path$img_start $server_path$img_path$img_end"; $makeIMG = `$tmpMakeIMG`; //DISPLAY THE IMAGE OR RETURN ERROR MESSAGE; USE FOR TESTING; DELETE THIS CODE IF NOT REQUIRED if (is_file($server_path.$img_path.$img_end)) { $img_size = getimagesize($img_path.$img_end); return $img = '<IMG SRC="'.$img_path.$img_end.'" border="0" '.$img_size[3].'>'; } else { return $error = "ERROR: There was a problem converting the image!"; } } else { return $error = "ERROR: $img_start file not found! Verify the server_path and img_path variables."; } } //EXAMPLE USAGE -- ONLY UN-COMMENT THE ONE YOU WANT TO USE: //OUTPUTS A FILE THAT IS PROPORTIONALLY RESIZED AND CROPPED TO 200px by 200px CALLED new.jpg //print convert_img ("original.jpg", "new.jpg", "200", "200", "", "yes"); //OUTPUTS A FILE THAT IS PROPORTIONALLY RESIZED TO EITHER 200px by X -OR- X by 200px (DEPENDING ON THE ASPECT RATIO) CALLED new.jpg //print convert_img ("original.jpg", "new.jpg", "200", "200", "", ""); //OUTPUTS A FILE THAT IS PROPORTIONALLY RESIZED TO 200px by X CALLED original.png //print convert_img ("original.jpg", "", "200", "", "png", ""); //OUTPUTS A FILE THAT IS PROPORTIONALLY RESIZED TO 200px by X CALLED original.png (alternate way of accomplishing the above) //print convert_img ("original.jpg", "original.png", "200", "", "", ""); //OVERWRITES THE ORIGINAL FILE AND RESIZES TO X by 200px //print convert_img ("original.jpg", "", "", "200", "", ""); ?> Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-442904 Share on other sites More sharing options...
theCro Posted January 18, 2008 Author Share Posted January 18, 2008 thanks so much... how do i use path to image in browser by setting size.. like www.mysite.com/foto.php image.jpg w=500 h= 300 for example? Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-443109 Share on other sites More sharing options...
harley1387 Posted January 19, 2008 Share Posted January 19, 2008 1. Configure the PHP file as instructed per the commented text 2. Put your pics in a directory called "images" on the same level as the php file 3. Create a new line to call the function in the php script at the bottom e.g.: print convert_img ($_GET['img_start'], $_GET['img_end'], $_GET['w'], $_GET['h'], "", $_GET['crop']); 4. Call the script via a URL: http://www.yourdomain.com/convert.php?img_start=original.jpg&img_end=new.jpg&w=300&h=200&crop=yes (assumes you've named the php file "convert.php") If you have a file called original.jpg in your images directory, the script will proportionally resize it to 300x200 and crop if necessary. The resulting file will be called new.jpg. Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-443518 Share on other sites More sharing options...
theCro Posted January 19, 2008 Author Share Posted January 19, 2008 1. Configure the PHP file as instructed per the commented text 2. Put your pics in a directory called "images" on the same level as the php file 3. Create a new line to call the function in the php script at the bottom e.g.: print convert_img ($_GET['img_start'], $_GET['img_end'], $_GET['w'], $_GET['h'], "", $_GET['crop']); 4. Call the script via a URL: http://www.yourdomain.com/convert.php?img_start=original.jpg&img_end=new.jpg&w=300&h=200&crop=yes (assumes you've named the php file "convert.php") If you have a file called original.jpg in your images directory, the script will proportionally resize it to 300x200 and crop if necessary. The resulting file will be called new.jpg. tried that, fixed all paths appropirately and it doesent work! http://www.zadarsport.com/foto.php?img_start=acogov1.jpg&img_end=new.jpg&w=300&h=200&crop=yes shows nothing and new.jpg isnt created at all! Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-443648 Share on other sites More sharing options...
harley1387 Posted January 19, 2008 Share Posted January 19, 2008 Should work...I've tested elsewhere. URL format is correct. 1. Where is your images directory? ...and what is your $img_path variable? The image you're trying to resize is not located at: http://www.zadarsport.com/images/acogov1.jpg 2. Have you changed the full $server_path variable -- not just the domain part? ...is it correct? 3. Are you 100% certain that ImageMagick is installed on your server? If so, have you changed the path accordingly? Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-443868 Share on other sites More sharing options...
theCro Posted January 19, 2008 Author Share Posted January 19, 2008 Should work...I've tested elsewhere. URL format is correct. 1. Where is your images directory? ...and what is your $img_path variable? The image you're trying to resize is not located at: http://www.zadarsport.com/images/acogov1.jpg 2. Have you changed the full $server_path variable -- not just the domain part? ...is it correct? 3. Are you 100% certain that ImageMagick is installed on your server? If so, have you changed the path accordingly? it's http://www.zadarsport.com/foto/acogov1.jpg i setted up as foto folder above at path. Quote Link to comment https://forums.phpfreaks.com/topic/86272-solved-help-needed-with-cropping/#findComment-443876 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.