Hello everyone! i am using this php script to create new image from original source. However, i'd like to use it different way... NOT to create new image but to use resized image 'on the fly', so the image itself is not modified but the one 'on the fly' is just with new dimensions... example how it works on the other site:
http://imagehandler4.index.hr/index.hr/c.790x350/images2/danijel_pranjic_index_velika600.jpg.jpe
if you change 790x350 it automatically resizes the image, but the image , when you look at it, remains the same size as it was originally uploaded:
http://www.index.hr/images2/danijel_pranjic_index_velika600.jpg
.... anyway, here is my code: SAVE IT AS generator.php, and your path where the photos are must be 'foto' or any you want but then inside php code change the path!
<?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']);
?>
you run it this way: http://www.yoursite.com/generator.php?img_start=acoipav2.jpg&img_end=xxaa.jpg&w=188&h=350&crop=yes
hopefully someone can help.. thanks! alot! 😃