img-resize-code.php
<!--image resize--> <?php require('img_resize_thumb.php'); $obj = new image_resize_thumb(); $obj->set_img_thumb_max_width(210); $obj->set_img_thumb_max_height(210); $obj->image_file_path("source_folder_path/","destination_folder_path/"); ?>
img_resize_thumb.php
<?PHP class image_resize_thumb { var $set_img_thumb_max_width; var $set_img_thumb_max_height; var $img_new_thumb_width; var $img_new_thumb_height; var $mime; var $image; var $img_thumb_width; var $img_thumb_height; var $img_path; var $img_thumb_save_path; var $img; function image_file_path($img_file_path,$img_thumbnail_path){ $img_type = array("jpg", "jpeg", "png", "gif"); $files = scandir($img_file_path); array_shift($files); array_shift($files); foreach($files as $key=>$value){ $ext = pathinfo($value, PATHINFO_EXTENSION); if (in_array($ext, $img_type)) { $this->img_path = $img_file_path.$value; $this->img_thumb_save_path = $img_thumbnail_path.$value; $this->img_resize_to_thumb(); } } } function set_img_thumb_max_width($img_thumb_width) { $this->set_img_thumb_max_width = $img_thumb_width; } function set_img_thumb_max_height($img_thumb_height) { $this->set_img_thumb_max_height = $img_thumb_height; } function get_mime() { $img_data = getimagesize($this->img_path); $this->mime = $img_data['mime']; } function image_create() { switch($this->mime) { case 'image/jpeg': $this->image = imagecreatefromjpeg($this->img_path); break; case 'image/gif': $this->image = imagecreatefromgif($this->img_path); break; case 'image/png': $this->image = imagecreatefrompng($this->img_path); break; } } function img_resize_to_thumb() { set_time_limit(0); $this->get_mime(); $this->image_create(); $this->img_thumb_width = imagesx($this->image); $this->img_thumb_height = imagesy($this->image); $this->img_set_dimension(); $resized_image = imagecreatetruecolor($this->img_new_thumb_width,$this->img_new_thumb_height); imagecopyresampled($resized_image, $this->image, 0, 0, 0, 0, $this->img_new_thumb_width, $this->img_new_thumb_height,$this->img_thumb_width, $this->img_thumb_height); imagejpeg($resized_image,$this->img_thumb_save_path); } function img_set_dimension() { if($this->img_thumb_width==$this->img_thumb_height) { $case = 'c1'; } elseif($this->img_thumb_width > $this->img_thumb_height) { $case = 'c2'; } else { $case = 'c3'; } if($this->img_thumb_width>$this->set_img_thumb_max_width && $this->img_thumb_height>$this->set_img_thumb_max_height) { $cond = 'c1'; } elseif($this->img_thumb_width>$this->set_img_thumb_max_width && $this->img_thumb_height<=$this->set_img_thumb_max_height) { $cond = 'c1'; } else { $cond = 'c3'; } switch($case) { case 'c1': $this->img_new_thumb_width = $this->set_img_thumb_max_width; $this->img_new_thumb_height = $this->set_img_thumb_max_height; break; case 'c2': $img_ratio = $this->img_thumb_width/$this->img_thumb_height; $amount = $this->img_thumb_width - $this->set_img_thumb_max_width; $this->img_new_thumb_width = $this->img_thumb_width - $amount; $this->img_new_thumb_height = $this->img_thumb_height - ($amount/$img_ratio); break; case 'c3': $img_ratio = $this->img_thumb_height/$this->img_thumb_width; $amount = $this->img_thumb_height - $this->set_img_thumb_max_height; $this->img_new_thumb_height = $this->img_thumb_height - $amount; $this->img_new_thumb_width = $this->img_thumb_width - ($amount/$img_ratio); break; } } } ?>
0 Comments