查看文件: img2th.php
大小: 3.43 KB
类型: application/octet-stream
<?php require_once('../Connections/BE2.php'); ?> <?php function ImageResize($from_filename, $save_filename, $in_width=108, $in_height=108, $quality=100) { $allow_format = array('jpeg', 'png', 'gif'); $sub_name = $t = ''; // Get new dimensions $img_info = getimagesize($from_filename); $width = $img_info['0']; $height = $img_info['1']; $imgtype = $img_info['2']; $imgtag = $img_info['3']; $bits = $img_info['bits']; $channels = $img_info['channels']; $mime = $img_info['mime']; list($t, $sub_name) = split('/', $mime); if ($sub_name == 'jpg') { $sub_name = 'jpeg'; } if (!in_array($sub_name, $allow_format)) { return false; } // 取得縮在此範圍內的比例 $percent = getResizePercent($width, $height, $in_width, $in_height); $new_width = $width * $percent; $new_height = $height * $percent; // Resample $image_new = imagecreatetruecolor($new_width, $new_height); // $function_name: set function name // => imagecreatefromjpeg, imagecreatefrompng, imagecreatefromgif /* // $sub_name = jpeg, png, gif $function_name = 'imagecreatefrom'.$sub_name; $image = $function_name($filename); //$image = imagecreatefromjpeg($filename); */ if($sub_name == 'jpeg'){ $image = imagecreatefromjpeg($from_filename); imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return imagejpeg($image_new, $save_filename, $quality); } else if($sub_name == 'png'){ $image = imagecreatefrompng($from_filename); imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return imagepng($image_new, $save_filename, 9); } } /** * 抓取要縮圖的比例 * $source_w : 來源圖片寬度 * $source_h : 來源圖片高度 * $inside_w : 縮圖預定寬度 * $inside_h : 縮圖預定高度 * * Test: * $v = (getResizePercent(1024, 768, 400, 300)); * echo 1024 * $v . "\n"; * echo 768 * $v . "\n"; */ function getResizePercent($source_w, $source_h, $inside_w, $inside_h) { if ($source_w < $inside_w && $source_h < $inside_h) { return 1; // Percent = 1, 如果都比預計縮圖的小就不用縮 } $w_percent = $inside_w / $source_w; $h_percent = $inside_h / $source_h; return ($w_percent > $h_percent) ? $h_percent : $w_percent; } ?> <? ini_set('date.timezone','Asia/Taipei'); ini_set('max_execution_time', 300); //300 seconds = 5 minutes $image_path="/www2/skybni/admin/upload_image/"; $save_path="/www2/skybni/admin/thumbnail/"; mysql_select_db($database_BE2, $BE2); $query_Rt2 = "SELECT * FROM product_data WHERE 1"; $Rt2 = mysql_query($query_Rt2, $BE2) or die(mysql_error()); $row_Rt2 = mysql_fetch_assoc($Rt2); $totalRows_Rt2 = mysql_num_rows($Rt2); echo $totalRows_Rt2 .'<br>'; do { $img=$image_path.$row_Rt2['meta_img']; $save=$save_path.$row_Rt2['meta_img']; $bol='N'; if(!file_exists($save)){ if(file_exists($img)){ $bol='Y'; ImageResize($img,$save); $c=0; while(1){ if(file_exists($save)){ break; } else{ $c++; } if($c>10000){ break;} } } } echo "\t".$bol."\t".$row_Rt2['meta_img'].'<br>'; } while ($row_Rt2 = mysql_fetch_assoc($Rt2)); mysql_free_result($Rt2); ?>
保存
取消
返回文件列表