[英]imagecopyresampled converts image to black
我試圖在上傳后調整圖像大小,但圖像總是轉換為黑色。 雖然圖像具有正確的尺寸,但它只顯示黑色。 我不確定我搞砸了哪里。 我已經搜索過SO無濟於事。
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $full_path)){
$orig_image = imagecreatefromjpeg($full_path);
$image_info = getimagesize($full_path);
$width_orig = $image_info[0]; // current width as found in image file
$height_orig = $image_info[1]; // current height as found in image file
if($width_orig > $height_orig){
$width = 105;
$ratio = $width/$width_orig;
$height = $height_orig*$ratio;
}else{
$height = 360;
$ratio = $height/$height_orig;
$width = $width_orig*$ratio;
}
$destination_image = imagecreatetruecolor($width, $height);
imagecopyresampled($destination_image, $orig_image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($destination_image, $full_path, 100);
}
請先檢查函數imagecreatefromjpeg()之前的圖像類型,以確保正確。 像這樣的東西:
$image_type = exif_imagetype($full_path);
switch ($image_type)
{
case IMAGETYPE_GIF : $orig_image = imagecreatefromgif($file); break;
case IMAGETYPE_JPEG : $orig_image = imagecreatefromjpeg($file); break;
case IMAGETYPE_PNG : $orig_image = imagecreatefrompng($file); break;
//...
default: echo 'Wrong image type'; break;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.