douju9272 2017-05-05 03:07
浏览 73

exif数据,php上传图片轮换

I'm having a hard time implemeting the php code will ensure that all uploaded photos will be oriented correctly upon upload.

here is my upload.php function ...

function process_image_upload($field)
{

if(!isset($_FILES[$field]['tmp_name']) || ! $_FILES[$field]['name'])
{
    return 'empty';
}

$temp_image_path = $_FILES[$field]['tmp_name'];
$temp_image_name = $_FILES[$field]['name'];
list($iwidth, $iheight, $temp_image_type) = 
getimagesize($temp_image_path);
if ($temp_image_type === NULL) {
    return false;
}
elseif( $iwidth < 400 )
    return 'size';
switch ($temp_image_type) {
    case IMAGETYPE_GIF:
        break;
    case IMAGETYPE_JPEG:
        break;
    case IMAGETYPE_PNG:
        break;
    default:
        return false;
}

$uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;
move_uploaded_file($temp_image_path, $uploaded_image_path);
$thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . 
preg_replace('{\\.[^\\.]+$}', '.jpg', $temp_image_name);

$main_image_path = MAIN_IMAGE_DESTINATION . preg_replace('{\\.[^\\.]+$}', '.jpg', $temp_image_name);

$result = 

 generate_image_thumbnail($uploaded_image_path,
$thumbnail_image_path,150,150
);  if( $result )
    $result = 
generate_image_thumbnail($uploaded_image_path,$main_image_path,400,400, 
true);
return $result ? array($uploaded_image_path, $thumbnail_image_path) : 
false;
}
if(isset($_POST['upload'])):
$result = process_image_upload('image');
if ($result === false) {
update_option('meme_message','<div class="error" style="margin-
left:0;"><p>An error occurred while processing upload</p></div>');
} else if( $result === 'empty')
{
        update_option('meme_message','<div class="error" style="margin-
 left:0;"><p>Please select a Image file</p></div>');

}
elseif( $result === 'size')
{
update_option('meme_message','<div class="error" style="margin-
left:0;"><p>Image width must be greater than 400px;</p></div>');
}
else {
    update_option('meme_message','<div style="margin-left:0;" 
class="updated"><p>Image uploaded successfully</p></div>');
    $guploaderr = false;
    $count      = intval(get_option('meme_image_count'));
    update_option('meme_image_count', $count+1);
}
endif;

and here is some php that I know will work to rotate... but I don't know how to implement.

<?php
$image = 
imagecreatefromstring(file_get_contents($_FILES['image_upload']
['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
    case 8:
        $image = imagerotate($image,90,0);
        break;
    case 3:
        $image = imagerotate($image,180,0);
        break;
    case 6:
        $image = imagerotate($image,-90,0);
        break;
    }
}
// $image now contains a resource with the image oriented correctly
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来