ZapMachine/lib/ImgTools.class.php

174 lines
4.8 KiB
PHP
Raw Normal View History

<?php
/**
* Library of tool functions to manipulate an image
*/
class ImgTools {
public static $minFuzzFactor = 0.08; //minimal factor for $fuzz
public static $maxFuzzFactor = 0.64; //maximal factor for $fuzz
/**
* Returns $im with random transparent color
* @param Imagick $im
* @param int $maxRGB
* @return Imagick $im
*/
public static function transparantRandom(Imagick $img, $maxRGB) {
//compute random fuzzyness
$minFuzz = intval(self::$minFuzzFactor * $maxRGB);
$maxFuzz = intval(self::$maxFuzzFactor * $maxRGB);
$randFuzz = mt_rand ($minFuzz,$maxFuzz);
//take average RGB value of random pixel
$xPoint = mt_rand(0,$img->getImageWidth());
$yPoint = mt_rand(0,$img->getImageHeight());
$randomPixel = $img->getImagePixelColor($xPoint, $yPoint);
$randomPixelColor = $randomPixel->getColorAsString();
//make transparency
$img->transparentPaintImage($randomPixelColor, 0.0, $randFuzz, false);
return $img;
}
/**
* random Rotate image
* @param Imagick $im
* @return Imagick $im
*/
public static function randomRotate(Imagick $img) {
$transp = new ImagickPixel('#ffffff');
$ar = array(0,90,180,270);
$img->rotateImage($transp, $ar[mt_rand(0,3)]);
return $img;
}
/**
* Resize image
* @param Imagick $im
* @param integer $retWidth
* @param integer $retHeight
* @return Imagick $im
*/
public static function resize(Imagick $img, $retWidth, $retHeight) {
$img->resizeImage((int)$retWidth, (int)$retHeight, imagick::FILTER_POINT, 0);
return $img;
}
/**
* Recolor image
* @param Imagick $im
* @return Imagick $im
*/
public static function recolor(Imagick $img){
$recolorValue = mt_rand(4, 250);
$times = mt_rand(1, 4);
for($i = 0; $i <= $times; $i++) {
$img->cycleColormapImage($recolorValue);
}
return $img;
}
/**
* returns MagickWand with randomly zoomed part of image $im with size $retWidth * $retHeight
* @param Imagick $im
* @param integer $retWidth
* @param integer $retHeight
* @param boolean $rotate
*/
public static function zoomCrop(Imagick $img, $retWidth, $retHeight, $rotate = false) {
if ($rotate == true) {
$img = self::randomRotate($img);
}
$fact = mt_rand(10, 40) / 100; //zoomfactor 10 - 40%
$width = $img->getImageWidth();
$height = $img->getImageHeight();
$newWidth = (int)($width * $fact);
$newHeight = (int)($height * $fact);
//crop $im
//x and y offset random 0 - width (or height) of blown up image minus crop area (of course!)
$img->cropImage($newWidth, $newHeight,
mt_rand(0, (int)($width - $newWidth)),
mt_rand(0, (int)($height - $newHeight)));
$img->resizeImage((int)$retWidth, (int)$retHeight, imagick::FILTER_POINT, 0);
return $img;
}
/**
* converts all gif images in array to png images
* returns Array with converted filenames
* @param array $imgAr
* @return array $newArray
*/
public static function convertGifsToPng($imgAr) {
$newArray = array();
while ($filename = array_shift($imgAr)) {
$imgInfo = getimagesize($filename);
$mimeType = $imgInfo['mime'];
//find gif
if ($mimeType == "image/gif") {
// Replace .gif extension with .png
$newFilename = preg_replace('/\.gif$/i', '.png', $filename);
//convert
$img = new Imagick($filename);
if($img->writeImage($newFilename)) {
//Remove original and rename new to save space
unlink($filename);
array_push($newArray, $newFilename);
}
} else {
array_push($newArray, $filename);
}
}
return $newArray;
}
/**
* LEGACY?
* INT (0 - 1000) -> promilage of average RGB value of center pixel of $im related to $maxRGB
* @param Imagick $im
* @param int $maxRGB
* @return int
*/
public static function imgCenterValueProm(Imagick $img, $maxRGB) {
$centerX = $img->getImageWidth() / 2;
$centerY = $img->getImageHeight() / 2;
//take average of the RGB of most middle pixel of each image and put it into an array
$centerPixel = $img->getImagePixelColor($centerX,$centerY);
$colors = $centerPixel->getColor();
$averageColor = round( ($colors['r'] + $colors['g'] + $colors['b']) / 3);
return round(($averageColor / $maxRGB) * 1000);
}
/**
* LEGACY?
* returns magickWand scaled proportionaly to $maxW or $maxH
* @param Imagick $im
* @param integer $maxW
* @param integer $maxH
* @return Imagick $im
*/
public static function scaleByLength(Imagick $img, $maxW, $maxH) {
$width = $img->getImageWidth();
$height = $img->getImageHeight();
if(($width <= ZAP_DEFAULT_WIDTH) && ($height <= ZAP_DEFAULT_HEIGHT)) {
return $img;
} else {
if ($width >= $height) { //horizontal image
$new_x = $maxW;
$new_y = round(($new_x / $width) * $height, 0);
} else { //vertical image
$new_y = $maxH;
$new_x = round(($new_y / $height) * $width, 0);
}
return self::resize($img, $new_x, $new_y);
}
}
}
?>