Code: Alles auswählen
Postby jcocking » Sat Feb 16, 2013 9:04 pm
I got bored and wrote the modification myself. I have attached the code here for your use if you are looking for the same modification.
No Warranty. No Support. Use at your own Risk. Backup Everything.
This code snippets does the following.
1. It will resize large files. The code will look at the width/height and compare to default setting defined in the function ImageResizeAndOrient(). You can change the defaults within the code.
2. The resize will work on gif, jpg and png files. The resulting saved file is a jpg.
3. If the file is a jpg, the code will look at the exif data and change the orientation of the picture to be positioned correctly. The save removes the EXIF to keep the picture correctly.
Files to Edit:
includes/functions_upload.php
Edits:
Find:
Code: Select all
/**
* Performing additional checks
Before Add:
Code: Select all
//Added new image functions
/**
* ImageHandler - ImageResizeAndOrient()
* Function will resize images and correct orientation
*
*/
function ImageResizeAndOrient()
{
// Edit values to control functionality
$max_width = 1200;
$max_height = 1000;
// No need to edit beyond this point
//get the image size
$destinationImage = false;
$size = getimagesize($this->destination_file);
//determine dimensions
$width = $size[0];
$height = $size[1];
if ($width > $max_width || $height > $max_height) {
ini_set("memory_limit","128M");
//determine what the file extension of the source
switch($this->extension)
{
case 'gif': case 'GIF':
$sourceImage = imagecreatefromgif($this->destination_file);
break;
case 'jpg': case 'JPG': case 'jpeg':
$sourceImage = imagecreatefromjpeg($this->destination_file);
break;
case 'png': case 'PNG':
$sourceImage = imagecreatefrompng($this->destination_file);
break;
}
// find the largest dimension of the image
// then calculate the resize perc based upon that dimension
$percentage = ( $width >= $height ) ? $max_width/$width : $max_height/$height;
// define new width / height
$newWidth = round($width * $percentage);
$newHeight = round($height * $percentage);
// create a new image
$destinationImage = imagecreatetruecolor($newWidth, $newHeight);
// copy resampled
imagecopyresampled($destinationImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
}
if ($this->extension == "jpg" || $this->extension == "jpeg") {
$flip=$rotate=false;
if (!$destinationImage) $destinationImage = imagecreatefromjpeg($this->destination_file);
//evaluate exif data from photo
$exif = exif_read_data($this->destination_file);
//get the orientation
$ort = $exif['Orientation'];
//determine what oreientation the image was taken at
switch($ort)
{
case 2: // horizontal flip
$flip = true;
break;
case 3: // 180 rotate left
$rotate = 180;
break;
case 4: // vertical flip
$flip = true;
$rotate = 180;
break;
case 5: // vertical flip + 90 rotate right
$flip = true;
$rotate = 90;
break;
case 6: // 90 rotate right
$rotate = -90;
break;
case 7: // horizontal flip + 90 rotate right
$flip = true;
$rotate = -90;
break;
case 8: // 90 rotate left
$rotate = 90;
break;
}
// create the jpegs
if ($flip) $this->ImageFlip($destinationImage);
if ($rotate) $destinationImage = imagerotate($destinationImage, $rotate, -1);
}
if ($destinationImage) {
imagejpeg($destinationImage, $this->destination_file, 100);
$this->extension = "jpg";
return;
} else {
return;
}
}
/**
* ImageHandler - ImageFlip()
*
*/
function ImageFlip(&$image, $x = 0, $y = 0, $width = null, $height = null)
{
if ($width < 1) $width = imagesx($image);
if ($height < 1) $height = imagesy($image);
// Truecolor provides better results, if possible.
if (function_exists('imageistruecolor') && imageistruecolor($image))
{
$tmp = imagecreatetruecolor(1, $height);
} else {
$tmp = imagecreate(1, $height);
}
$x2 = $x + $width - 1;
for ($i = (int)floor(($width - 1) / 2); $i >= 0; $i--)
{
// Backup right stripe.
imagecopy($tmp, $image, 0, 0, $x2 - $i, $y, 1, $height);
// Copy left stripe to the right.
imagecopy($image, $image, $x2 - $i, $y, $x + $i, $y, 1, $height);
// Copy backuped right stripe to the left.
imagecopy($image, $tmp, $x + $i, $y, 0, 0, 1, $height);
}
imagedestroy($tmp);
return true;
}
Find:
Code: Select all
phpbb_chmod($this->destination_file, $chmod);
Before Add:
Code: Select all
// Check image orientation
if ($this->is_image()) $this->ImageResizeAndOrient();
Vielleicht kann einer der Serveradmins das verwenden?