theCro Posted July 12, 2008 Share Posted July 12, 2008 Hello. Some of you fellows already helped me with cropping method .. i use it and it really rocks. However, when cropping it creates NEW file.. i'd like to avoid this as i get into malicious problem when people discover the image patch they easily change width and height and image gets corrupted by their malicious offsets... the code i use is this one: <?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 = "/home/zadarsp/public_html/"; //PATH TO IMAGES DIRECTORY $img_path = "foto/"; //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/bin/convert -density 300X300 -quality 85 $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 = "Greska: $img_start ne moze biti pronadjen! Provjeri server_path i img_path varijable."; } } //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", "", ""); print convert_img ($_GET['img_start'], $_GET['img_end'], $_GET['w'], $_GET['h'], "", $_GET['crop']); ?> can someone modify it so it's possible to catch image 'on the fly' without changing the original image and creating new one? thanks alot! Link to comment https://forums.phpfreaks.com/topic/114424-help-needed-with-cropping-on-the-fly-code-is-here/ Share on other sites More sharing options...
theCro Posted July 12, 2008 Author Share Posted July 12, 2008 anyone? :'( Link to comment https://forums.phpfreaks.com/topic/114424-help-needed-with-cropping-on-the-fly-code-is-here/#findComment-588520 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.