TinyMCE显示PHP旋转后宽度和高度相同的图像
此PHP脚本正在处理上载并使用TinyMCE编辑器。但是,如果图像具有EXIF方向元数据,则会出现问题。图像旋转正确,但仍会显示宽度和高度为wronf的图像。我需要将新的宽度和高度传递给TinyMCE,但不确定如何传递TinyMCE显示PHP旋转后宽度和高度相同的图像,tinymce,height,width,exif,Tinymce,Height,Width,Exif,此PHP脚本正在处理上载并使用TinyMCE编辑器。但是,如果图像具有EXIF方向元数据,则会出现问题。图像旋转正确,但仍会显示宽度和高度为wronf的图像。我需要将新的宽度和高度传递给TinyMCE,但不确定如何传递 <?php //error_reporting(0); //Only these origins are allowed to upload images * $accepted_origins = array("http://localhost"
<?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, 0, 0, 0, 0, $newimgwidth, $newimgheight, $imgwidth, $imgheight);
}
// then rewrite the rotated image back to the disk as $filename
imagejpeg($img, $savetmpname, 75);