问题描述
此PHP脚本正在处理上载并与TinyMCE编辑器一起使用。但是,如果图像具有EXIF方向元数据,则会出现问题。图像已正确旋转,但TinyMCE显示的图像的宽度和高度都很高。我需要将新的宽度和高度传递给TinyMCE,但不确定如何做?
<?PHP
//error_reporting(0);
//Only these origins are allowed to upload images *
$accepted_origins = array("http://localhost","http://127.0.0.1","http://192.168.1.1","http://example.com");
// Set the destination folder for image
$month = strtolower(date('M'));
$year = date('Y');
$newspath = "img/news/";
if (!is_dir("$newspath/$year/$month")) {
mkdir("$newspath/$year/$month",0777,true);
}
$imageFolder = "$newspath/$year/$month/";
reset ($_FILES);
$temp = current($_FILES);
if (is_uploaded_file($temp['tmp_name'])){
if (isset($_SERVER['HTTP_ORIGIN'])) {
// same-origin requests won't set an origin. If the origin is set,it must be valid.
if (in_array($_SERVER['HTTP_ORIGIN'],$accepted_origins)) {
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
} else {
header("HTTP/1.1 403 Origin Denied");
return;
}
}
/*
If your script needs to receive cookies,set images_upload_credentials : true in
the configuration and enable the following two headers.
*/
// header('Access-Control-Allow-Credentials: true');
// header('P3P: CP="There is no P3P policy."');
// Rewrite bad filenames
$temp['name'] = preg_replace(array('/\s+/','/[^a-zA-Z0-9\-\._]/','/\.(?=.*\.)/','/[\-]+/','/[_]+/'),array('_','','_','_'),strtolower($temp['name']));
// Verify extension
if (!in_array(strtolower(pathinfo($temp['name'],PATHINFO_EXTENSION)),array("gif","jpg","jpeg","png"))) {
header("HTTP/1.1 400 Invalid extension.");
return;
}
// Accept upload if there was no origin,or if it is an accepted origin
$tempname = $temp['name'];
$filetowrite = $imageFolder . $tempname;
$randompin = mt_rand(1000,9999);
// If file name already exists rename
if (file_exists($filetowrite)) {
$tempname = $randompin . $tempname;
$filetowrite = $imageFolder . $tempname;
}
// If EXIF Orientation data exists,rotate image as required
$savetmpname = $temp['tmp_name'];
function correctimageOrientation($savetmpname) {
if (function_exists('exif_read_data')) {
$exif = exif_read_data($savetmpname);
if($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if($orientation != 1){
$img = imagecreatefromjpeg($savetmpname);
$deg = 0;
switch ($orientation) {
case 3:
$deg = 180;
break;
case 6:
$deg = 270;
break;
case 8:
$deg = 90;
break;
}
if ($deg) {
$img = imagerotate($img,$deg,0);
}
//Rewrite rotated image back to the disk
imagejpeg($img,$savetmpname,75);
} // if there is some rotation necessary
} // if have the exif orientation info
} // if function exists
}
// Move uploaded files and rotate if required
move_uploaded_file($savetmpname,$filetowrite);
correctimageOrientation($filetowrite);
//}
// Respond to the successful upload with JSON.
// Use a location key to specify the path to the saved image resource.
// { location : '/your/uploaded/image/file'}
// echo json_encode(array('location' => $filetowrite));
$passpath = $year.'/'.$month.'/'.$tempname;
echo json_encode(array('location' => $passpath));
} else {
// Notify editor that the upload Failed
header("HTTP/1.1 500 Server Error");
}
?>
解决方法
我发现了这一点,可以通过将图像旋转90度或270度来交换图像的宽度和高度,这是exif函数:
// If EXIF Orientation data exists,rotate image as required
$savetmpname = $temp['tmp_name'];
function correctImageOrientation($savetmpname) {
if (function_exists('exif_read_data')) {
$exif = exif_read_data($savetmpname);
if($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if($orientation != 1){
$img = imagecreatefromjpeg($savetmpname);
$imgwidth = imagesx($img);
$imgheight = imagesy($img);
//$imgwidth = ImagesX($img);
//$imgheight = ImagesY($img);
$deg = 0;
switch ($orientation) {
case 3:
$deg = 180;
$newimgwidth = $imgwidth;
$newimgheight = $imgheight;
break;
case 6:
$deg = 270;
$newimgwidth = $imgheight;
$newimgheight = $imgwidth;
break;
case 8:
$deg = 90;
$newimgwidth = $imgheight;
$newimgheight = $imgwidth;
break;
}
if ($deg) {
$img = imagerotate($img,$deg,0);
$img = imagecopyresized($img,$savetmpname,$newimgwidth,$newimgheight,$imgwidth,$imgheight);
}
// then rewrite the rotated image back to the disk as $filename
imagejpeg($img,75);