orchet7
Technical User
- Jul 13, 2008
- 6
Hello everyone! i am using this code to generate thumbnail on the fly:
<code><?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']);
?>
</code>
and
to run it (you can change sizes from 99x50 it changes automatically)
HOWEVER, img_start=test2.jpg thing here... i dont want to use it this way ... it now works as you can see in php code.. it looks for server path, then folder path and then image.jpg part so at the end you only have to use image.jpg as it automatically fins folder and stuff...
however i'd like if someone can tweak my code so it opens image like this :
instead of:
many thanks in advance! p.s. it's free you can use it
<code><?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']);
?>
</code>
and
to run it (you can change sizes from 99x50 it changes automatically)
HOWEVER, img_start=test2.jpg thing here... i dont want to use it this way ... it now works as you can see in php code.. it looks for server path, then folder path and then image.jpg part so at the end you only have to use image.jpg as it automatically fins folder and stuff...
however i'd like if someone can tweak my code so it opens image like this :
instead of:
many thanks in advance! p.s. it's free you can use it