View Single Post
  #1  
Old 09-30-2003, 08:22 AM
 
eleven eleven is offline
 

Senior Member
  
Join Date: Nov 2002
Location: Charlotte, NC, USA
Posts: 118
 

Default Automatic Thumbnail Resize

A client of mine didn't understand how to resize his images before he added them to the cart, so I wrote a quick and dirty mod to automatically resize them when he uploads them.

WARNING:
- it only works for jpg's (although it can easily be changed)
- it only works for images stored in the file system
- it uses GD. Some servers may not have GD installed (most will)
- the width is hard coded, you need to change it to the width of your images. It is line number 39 below.
- it will not work for batch imports, only thumbnails added through the Modify Products section

In /cart/include/func.php replace the function func_get_image_content with this:

Code:
function func_get_image_content($file_upload_data, $id) { global $config; $file_aliases_count_max = 99; switch($file_upload_data["imtype"]) { case "C": $config_data["location"] = $config["Images"]["icons_location"]; break; case "W": $config_data["location"] = $config["Images"]["pcicons_location"]; break; case "T": $config_data["location"] = $config["Images"]["thumbnails_location"]; break; case "D": $config_data["location"] = $config["Images"]["det_images_location"]; break; default: return false; } $file_path = $file_upload_data["file_path"]; if ($fd = fopen($file_path, "rb")) { if ($config_data["location"] == "FS") { # # Resize image # if (($file_upload_data["imtype"] == "T") && ($file_upload_data["image_type"] == "image/pjpeg")) { $img_src = ImageCreateFromjpeg ($imgname); $true_width = imagesx($img_src); $true_height = imagesy($img_src); # change this next line to control the width of the thumbnail $width = 360; $height = ($width/$true_width)*$true_height; $img_des = imagecreatetruecolor($width,$height); imagecopyresized ($img_des, $img_src, 0, 0, 0, 0, $width, $height, $true_width, $true_height); imagejpeg($img_des,$file_path,80); } # end resize # # ...else image is path to file # $image = $file_path; } else { # # If image should be stored in the database, get image content from file # if ($file_upload_data["source"] == "U") $file_size = 1000000; else $file_size = filesize($file_path); $image = addslashes(fread($fd, $file_size)); } fclose($fd); if ($file_upload_data["source"] == "L" && !empty($file_upload_data["dir_upload"])) { if ($config_data["location"] == "FS") { # # For FS storing. If image has been uploaded, move it to specified directory # $file_name = ($file_upload_data["imtype"]=="W"?"w_$id":($file_upload_data["imtype"]=="C"?"c_$id":($file_upload_data["imtype"]=="T"?"t_$id":"d_$id"))); $file_type = (strstr($file_path,"gif")?"gif":(strstr($file_path,"png")?"png":"jpg")); # # Check the existing file # $counter = 1; $file_name_tmp = $file_name; while (file_exists($file_upload_data["dir_upload"]."/".$file_name_tmp.".".$file_type) && $counter<$file_aliases_count_max) { $file_name_tmp = $file_name."_".sprintf("%02d", $counter); $counter++; } $file_name = $file_name_tmp; $image = $file_upload_data["dir_upload"]."/".$file_name.".".$file_type; copy($file_path, $image); @chmod($image, 0666); } # # Delete temporary file # @unlink($file_path); } } return array("image"=>$image, "image_type"=>$file_upload_data["image_type"], "image_x"=>$width, "image_y"=>$height, "file_size"=>$file_upload_data["file_size"]); }

I'm sure someone with a little more time on their hands could develop this into a nice mod that works for all images, not just thumbnails and jpg's and gets its settings from the admin area.
__________________
|| E L E V E N ||
Reply With Quote