전체 이미지를 업로드 한 다음 표시하기 위해 미리보기 이미지로 만듭니다. 내 업로드를 처리하려면 다음 코드를 사용하고 있습니다 :이미지를 업로드하여 축소판 그림으로 만들면 이미지가 왜곡됩니다.
$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
스크립트가 이미지의 크기를 조정하지 않고 단순히 크기를 조정하는 중입니다. 종횡비를 유지하려면 몇 가지 수학을 수행하십시오. – Crashspeeder
@Mathieu 내 Wordpress 블로그에는 설정이 있으며 스크립트를 가져 오는 경우 스크립트 맨 위에 표시됩니다. – user1048676