<?php
/*
* Copyright 2008 Blandware (http://www.blandware.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Image utils.
*
* @package AtleapLite
* @author Roman Puchkovskiy
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
* Resizes image taken from file.
*
* @param string $fileName name of file from which to take image
* @param int $maxWidth optional max width; if omitted, width is
* not restricted
* @param int $maxHeight optional max height; if omitted, height is
* not restricted
* @return array 'type' => image type, 'data' => binary data, 'width' => width,
* 'height' => height
*/
function resizeImageFromFile($fileName, $maxWidth = null, $maxHeight = null) {
list($width, $height, $type, $attrs) = getimagesize($fileName);
$data = file_get_contents($fileName);
return resizeImage($data, $width, $height, $type, $maxWidth, $maxHeight);
}
/**
* Resizes image.
*
* @param string $data binary image data
* @param int $width image width
* @param int $height image height
* @param int $type image type
* @param int $maxWidth optional max width; if omitted, width is
* not restricted
* @param int $maxHeight optional max height; if omitted, height is
* not restricted
* @return array 'type' => image type, 'data' => binary data, 'width' => width,
* 'height' => height
*/
function resizeImage($data, $width, $height, $type,
$maxWidth = null, $maxHeight = null) {
if ($maxWidth === null) {
$maxWidth = 1000000;
}
if ($maxHeight === null) {
$maxHeight = 1000000;
}
$srcImg = imagecreatefromstring($data);
if ($width === null) {
$width = imagesx($srcImg);
}
if ($height === null) {
$height = imagesy($srcImg);
}
if ($width <= $maxWidth && $height <= $maxHeight) {
$imgType = $type;
} else {
// Need to shrink image
// Calculating dimentions
$widthScale = $width / $maxWidth;
$heightScale = $height / $maxHeight;
if ($widthScale > $heightScale) {
// Width will be max
$newWidth = $maxWidth;
$newHeight = round($height / $widthScale);
if ($newHeight > $maxHeight) {
$newHeight = $maxHeight;
}
if ($newHeight < 1) {
$newHeight = 1;
}
} else {
// Height will be max
$newHeight = $maxHeight;
$newWidth = round($width / $heightScale);
if ($newWidth > $maxWidth) {
$newWidth = $maxWidth;
}
if ($newWidth < 1) {
$newWidth = 1;
}
}
// Scaling image
$destImg = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($destImg, $srcImg, 0, 0, 0, 0,
$newWidth, $newHeight, $width, $height);
ob_start();
switch ($type) {
case IMAGETYPE_GIF:
imagepng($destImg);
break;
case IMAGETYPE_JPEG:
imagejpeg($destImg);
break;
case IMAGETYPE_PNG:
imagepng($destImg);
break;
case IMAGETYPE_WBMP:
imagewbmp($destImg);
break;
}
$data = ob_get_contents();
ob_end_clean();
imagedestroy($destImg);
if ($type == IMAGETYPE_GIF) {
$imgType = IMAGETYPE_PNG;
} else {
$imgType = $type;
}
$width = $newWidth;
$height = $newHeight;
}
imagedestroy($srcImg);
return array('type' => $imgType, 'data' => $data,
'width' => $width, 'height' => $height);
}
/**
* Deletes internal image.
*
* @param int $id internal image ID
*/
function deleteInternalImage($id) {
$dao =& getDao('internal_image');
$dao->id = $id;
$dao->delete();
}
?>