Php 基于EXIF数据上传和旋转图像

Php 基于EXIF数据上传和旋转图像,php,Php,目前我正在使用此代码上载图像: elseif ($_post['password'] == ''){ if($my_form->checkEmail($_POST['email'])) { // check for good mail $allowedExts = array("gif", "jpeg", "jpg", "png"); $extension = end(explode(".", $_FILES["img"]["name"]));

目前我正在使用此代码上载图像:

elseif ($_post['password'] == ''){
    if($my_form->checkEmail($_POST['email'])) { // check for good mail

$allowedExts = array("gif", "jpeg", "jpg", "png");

        $extension = end(explode(".", $_FILES["img"]["name"]));

        if ((($_FILES["img"]["type"] == "image/gif")
        || ($_FILES["img"]["type"] == "image/jpeg")
        || ($_FILES["img"]["type"] == "image/jpg")
        || ($_FILES["img"]["type"] == "image/pjpeg")
        || ($_FILES["img"]["type"] == "image/x-png")
        || ($_FILES["img"]["type"] == "image/png"))
        && ($_FILES["img"]["size"] < 3145728)
        && in_array($extension, $allowedExts))
        {
           if ($_FILES["img"]["error"] <= 0)
           {
              move_uploaded_file($_FILES["img"]["tmp_name"], "upload/" . $user->userID.'.'.$extension);
           }
        }
elseif($\u post['password']=''){
如果($my_form->checkEmail($_POST['email']){//检查好邮件
$allowedExts=数组(“gif”、“jpeg”、“jpg”、“png”);
$extension=end(分解(“.”,$_文件[“img”][“name”]);
如果(($_文件[“img”][“type”]=“image/gif”)
||($_文件[“img”][“type”]=“image/jpeg”)
||($_文件[“img”][“type”]=“image/jpg”)
||($_文件[“img”][“type”]=“image/pjpeg”)
||($_文件[“img”][“type”]=“image/x-png”)
||($_文件[“img”][“type”]=“image/png”))
&&($_文件[“img”][“size”]<3145728)
&&in_数组($extension$allowedExts))
{
if($_FILES[“img”][“error”]userID....$扩展名);
}
}
我正在使用此代码显示图像(它捕获最后修改的文件):

$avatars=glob(“上传/$user->userID.*);
如果(!空($avatars)){
$avatars=array_combine($avatars,array_map(“filemtime”,$avatars));
阿索尔(阿凡达);
$latest_avatar=key($avatar);
回声';
echo'**注意图像必须小于3MB。

'; 回声';
我已经看到了一些代码,并尝试使用它,但我无法让它工作…类似这样的东西:

$avatars = glob("upload/$user->userID.*");
if(!empty($avatars)) {
$avatars = array_combine($avatars, array_map("filemtime", $avatars));
arsort($avatars);
$latest_avatar = key($avatars);

function resample($latest_avatar, $thumbFile, $width, $orientation) {
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($latest_avatar);
$height = (int) (($width / $width_orig) * $height_orig);
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image   = imagecreatefromjpeg($latest_avatar);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Fix Orientation
switch($orientation) {
    case 3:
        $image_p = imagerotate($image_p, 180, 0);
        break;
    case 6:
        $image_p = imagerotate($image_p, -90, 0);
        break;
    case 8:
        $image_p = imagerotate($image_p, 90, 0);
        break;
}
// Output
imagejpeg($image_p, $thumbFile, 90);
}

echo '<img style="width:20%;" src="'.$image_p.'">';
echo '<p>**Note Image must be smaller than 3MB.</p>';
echo '<input type="submit" name="submit" value="Submit"></td></tr>';
$avatars=glob(“上传/$user->userID.*);
如果(!空($avatars)){
$avatars=array_combine($avatars,array_map(“filemtime”,$avatars));
阿索尔(阿凡达);
$latest_avatar=key($avatar);
函数重采样($latest_avatar、$thumbFile、$width、$orientation){
//获取新维度
列表($width\u orig,$height\u orig)=getimagesize($latest\u avatar);
$height=(int)($width/$width\u orig)*$height\u orig);
//重采样
$image\u p=imageCreateTureColor($width,$height);
$image=imagecreatefromjpeg($latest_avatar);
imagecopyresampled($image\u p,$image,0,0,0,$width,$height,$width\u orig,$height\u orig);
//固定方向
开关($方向){
案例3:
$image\u p=imagerotate($image\u p,180,0);
打破
案例6:
$image\u p=imagerotate($image\u p,-90,0);
打破
案例8:
$image\u p=imagerotate($image\u p,90,0);
打破
}
//输出
图像JPEG($image\u p,$thumbFile,90);
}
回声';
echo'**注意图像必须小于3MB。

'; 回声';
它没有显示任何内容,img src只是空白。我没有收到任何错误…错误报告已启用。如果我取出函数并使用
echo';
它工作正常并显示图像,但它当然不会旋转


有人能帮我解决这个问题吗?

这会失败的
$\u post
(看看你现在有什么电子邮件?)。请修复此问题,然后重试。您应该会收到一个未定义的变量通知,但您没有检查错误。此外,很难说您的表单是否符合犹太标准表单工作正常…我当前可以上载图像和其他所有内容。我只需要根据EXIF数据旋转图像,谢谢。好的,技术上讲,
$\u post['password']
应该是
$\u POST['password']
,但这可能与此处无关。在打开PHP标记后立即将错误报告添加到文件顶部,例如
,此问答有助于确定您的代码是否来自于此。这将失败
$\u POST
,原因之一是(请参阅您现在的电子邮件内容?)。请修复此问题,然后重试。您应该会收到一个未定义的变量通知,但您没有检查错误。此外,很难说您的表单是否符合犹太标准表单工作正常…我当前可以上载图像和其他所有内容。我只需要根据EXIF数据旋转图像,谢谢。好的,技术上讲,
$\u post['password']
应该是
$\u POST['password']
,但这可能与此处无关。在打开PHP标记后立即将错误报告添加到文件顶部,例如
,此问答有助于确定您的代码是否来自于此。
$avatars = glob("upload/$user->userID.*");
if(!empty($avatars)) {
$avatars = array_combine($avatars, array_map("filemtime", $avatars));
arsort($avatars);
$latest_avatar = key($avatars);

function resample($latest_avatar, $thumbFile, $width, $orientation) {
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($latest_avatar);
$height = (int) (($width / $width_orig) * $height_orig);
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image   = imagecreatefromjpeg($latest_avatar);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Fix Orientation
switch($orientation) {
    case 3:
        $image_p = imagerotate($image_p, 180, 0);
        break;
    case 6:
        $image_p = imagerotate($image_p, -90, 0);
        break;
    case 8:
        $image_p = imagerotate($image_p, 90, 0);
        break;
}
// Output
imagejpeg($image_p, $thumbFile, 90);
}

echo '<img style="width:20%;" src="'.$image_p.'">';
echo '<p>**Note Image must be smaller than 3MB.</p>';
echo '<input type="submit" name="submit" value="Submit"></td></tr>';