全部, 我正在上傳圖片,然後將其作爲縮略圖用於顯示目的。我使用下面的代碼來處理我上傳:將圖片上傳並製作爲縮略圖時出現圖片失真
$imageW = $blogOptions['image']['width'];
$imageH = $blogOptions['image']['height'];
$themePath = ABSPATH . 'wp-content/';
$path_to_image_directory = $themePath.'uploads/';
$path_to_thumbs_directory = $themePath.'uploads/thumbs/';
$fieldname = 'logo';
if(preg_match('/[.](jpg)|(gif)|(png)|(JPG)$/', $_FILES[$fieldname]['name'])) {
$now = time();
$filename = $now."_".$_FILES[$fieldname]['name'];
$source = $_FILES[$fieldname]['tmp_name'];
$target = $path_to_image_directory . $filename;
move_uploaded_file($source, $target);
$file = $path_to_image_directory . $filename;
$x = @getimagesize($file);
switch($x[2]) {
case 1:
$type = "gif";
break;
case 2:
$type = "jpeg";
break;
case 3:
$type = "png";
break;
default:
echo "error";
}
if($type == "gif"){
$im = imagecreatefromgif($path_to_image_directory . $filename);
}if($type == "jpeg"){
$im = imagecreatefromjpeg($path_to_image_directory . $filename);
}if($type == "png"){
$im = imagecreatefrompng($path_to_image_directory . $filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $imageW;
$ny = $imageH;
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
if(!file_exists($path_to_thumbs_directory)) {
if(!mkdir($path_to_thumbs_directory)) {
die("There was a problem. Please try again!");
}
}
if($type == "gif"){
imagegif($nm, $path_to_thumbs_directory . $filename);
}if($type == "jpeg"){
imagejpeg($nm, $path_to_thumbs_directory . $filename);
}if($type == "png"){
imagepng($nm, $path_to_thumbs_directory . $filename);
}
}
}
上傳工作正常,當我顯示它是圖像看起來極度扭曲的文件創建成功但是。如果我使用Wordpress處理文件上傳並創建縮略圖,那麼它根本不會看起來失真。有沒有更好的方法來上傳這個文件,或者我沒有錯,不會失去圖片質量?
謝謝!
如何獲得$ imageW和$ imageH? – Tchoupi 2012-02-24 20:39:04
腳本不縮放圖像,它只是調整大小。做一些數學來保持寬高比。 – Crashspeeder 2012-02-24 20:42:42
@Mathieu在我的WordPress博客中有設置,我把它們拉進來。如果你在腳本的頂部看到這些設置。 – user1048676 2012-02-24 20:57:33