/** * DOCUMENT ME * @param mixed $fileIn * @param mixed $fileOut * @param mixed $scaleParameters * @param mixed $cropParameters * @param mixed $quality * @return mixed */ private static function scaleGd($fileIn, $fileOut, $scaleParameters = array(), $cropParameters = array(), $quality = 75) { // gd version for those who can't install netpbm, poor buggers // "handles" PDF by rendering a blank white image. We already superimpose a PDF icon, // so this should work well // (if you can install ghostview, you can install netpbm too, so there's no middle case) // Special case to emit the original. This preserves transparency in GIFs and is faster for everything. (PNGs can always preserve // alpha channel in anything under 1024x768 or when gd is the only backend enabled.) WARNING: keep this up to date if new // capabilities are added - we need to make sure they are not active etc. before using this trick. TODO: check for this in // netpbm land too, right now in a typical configuration it's not checked over 1024x768 // Default to normal orientation $orientation = 1; $imageInfo = getimagesize($fileIn); // Don't panic on a PDF, fall through to the fake handler for that. if ($imageInfo) { $width = $imageInfo[0]; $height = $imageInfo[1]; $orientation = aImageConverter::getRotation($fileIn, $imageInfo); if ($imageInfo[2] === IMAGETYPE_JPEG) { // Some EXIF orientations swap width and height switch ($orientation) { case 5: // vertical flip + 90 rotate right // vertical flip + 90 rotate right case 6: // 90 rotate right // 90 rotate right case 7: // horizontal flip + 90 rotate right // horizontal flip + 90 rotate right case 8: // 90 rotate left $tmp = $width; $width = $height; $height = $tmp; break; } } $infoIn = pathinfo($fileIn); $infoOut = pathinfo($fileOut); // Try not to do any work if we are not changing anything if ($orientation == 1) { if ((!count($scaleParameters) || isset($scaleParameters['xysize']) && $scaleParameters['xysize'][0] == $width && $scaleParameters['xysize'][1] == $height) && strtolower($infoIn['extension']) === strtolower($infoOut['extension']) && !count($cropParameters)) { copy($fileIn, $fileOut); return true; } } } if (preg_match('/\\.pdf$/i', $fileIn)) { $in = self::createTrueColorAlpha(100, 100); imagefilledrectangle($in, 0, 0, 100, 100, imagecolorallocate($in, 255, 255, 255)); } else { $in = self::imagecreatefromany($fileIn); if ($orientation != 1) { // Note that gd rotation is CCL switch ($orientation) { case 2: // horizontal flip aImageConverter::horizontalFlip($in); break; case 3: // 180 rotate left $in2 = imagerotate($in, 180, imagecolorallocate($in, 255, 255, 255)); imagedestroy($in); $in = $in2; break; case 4: // vertical flip aImageConverter::verticalFlip($in); break; case 5: // vertical flip + 90 rotate right aImageConverter::verticalFlip($in); $in2 = imagerotate($in, 270, imagecolorallocate($in, 255, 255, 255)); imagedestroy($in); $in = $in2; break; case 6: // 90 rotate right $in2 = imagerotate($in, 270, imagecolorallocate($in, 255, 255, 255)); imagedestroy($in); $in = $in2; break; case 7: // horizontal flip + 90 rotate right aImageConverter::horizontalFlip($in); $in2 = imagerotate($in, 270, imagecolorallocate($in, 255, 255, 255)); imagedestroy($in); $in = $in2; break; case 8: // 90 rotate left $in2 = imagerotate($in, 90, imagecolorallocate($in, 255, 255, 255)); imagedestroy($in); $in = $in2; break; } } } if (!$in) { return false; } if (preg_match("/\\.(\\w+)\$/i", $fileOut, $matches)) { $extension = $matches[1]; $extension = strtolower($extension); } else { imagedestroy($in); return false; } $top = 0; $left = 0; $width = imagesx($in); $height = imagesy($in); if (count($cropParameters)) { if (isset($cropParameters['top'])) { $top = $cropParameters['top']; } if (isset($cropParameters['left'])) { $left = $cropParameters['left']; } if (isset($cropParameters['width'])) { $width = $cropParameters['width']; } if (isset($cropParameters['height'])) { $height = $cropParameters['height']; } $cropped = self::createTrueColorAlpha($width, $height); imagealphablending($cropped, false); imagesavealpha($cropped, true); imagecopy($cropped, $in, 0, 0, $left, $top, $width, $height); imagedestroy($in); $in = null; } else { // No cropping, so don't waste time and memory $cropped = $in; $in = null; } if (count($scaleParameters)) { $width = imagesx($cropped); $height = imagesy($cropped); $swidth = $width; $sheight = $height; if (isset($scaleParameters['xsize'])) { $height = $scaleParameters['xsize'] * imagesy($cropped) / imagesx($cropped); $width = $scaleParameters['xsize']; $out = self::createTrueColorAlpha($width, $height); imagecopyresampled($out, $cropped, 0, 0, 0, 0, $width, $height, imagesx($cropped), imagesy($cropped)); imagedestroy($cropped); $cropped = null; } elseif (isset($scaleParameters['ysize'])) { $width = $scaleParameters['ysize'] * imagesx($cropped) / imagesy($cropped); $height = $scaleParameters['ysize']; $out = self::createTrueColorAlpha($width, $height); imagecopyresampled($out, $cropped, 0, 0, 0, 0, $width, $height, imagesx($cropped), imagesy($cropped)); imagedestroy($cropped); $cropped = null; } elseif (isset($scaleParameters['scale'])) { $width = imagesx($cropped) * $scaleParameters['scale']; $height = imagesy($cropped) * $scaleParameters['scale']; $out = self::createTrueColorAlpha($width, $height); imagecopyresampled($out, $cropped, 0, 0, 0, 0, $width, $height, imagesx($cropped), imagesy($cropped)); imagedestroy($cropped); $cropped = null; } elseif (isset($scaleParameters['xysize'])) { $width = $scaleParameters['xysize'][0]; $height = $scaleParameters['xysize'][1]; // This was backwards until 05/31/2010, making things bigger rather than smaller if their // aspect ratios differed from the original. Be consistent with netpbm which makes things // smaller not bigger if ($width / $height > $swidth / $sheight) { // Wider than the original. So it will be narrower than requested $width = ceil($height * ($swidth / $sheight)); } else { // Taller than the original. So it will be shorter than requested $height = ceil($width * ($sheight / $swidth)); } $out = self::createTrueColorAlpha($width, $height); imagecopyresampled($out, $cropped, 0, 0, 0, 0, $width, $height, $swidth, $sheight); imagedestroy($cropped); $cropped = null; } } else { // No scaling, don't waste time and memory $out = $cropped; $cropped = null; } $extension = strtolower($infoOut['extension']); if ($extension === 'gif') { imagegif($out, $fileOut); } elseif ($extension === 'jpg' || $extension === 'jpeg') { imagejpeg($out, $fileOut, $quality); } elseif ($extension === 'png') { imagepng($out, $fileOut); } else { return false; } imagedestroy($out); $out = null; return true; }