Функции GD и функции для работы с изображениями
The image sharpen function (by Alex R. Austin) provided below seems to be very resource hungry and I couldn’t make it work on two different servers — trying to sharpen a 413 x 413 image I ended up with «Fatal error: Allowed memory size of 8388608 bytes exhausted» or «Internal Server Error» or the script terminated without notice. Because I had no priviliges to change the default memory limit on these servers I started looking for other sharpen functions. I have come across a php Unsharp Mask function which works like a charm on both of the servers I dealt with. It can be found at http://vikjavev.no/hovudsida/umtestside.php.
Representation decimal of a color in hexadecimal for use on functions of library GD.
// Representation hexadecimal
$var = ‘#FFFFFF’ ;
function getRgbFromGd ( $color_hex )
return array_map ( ‘hexdec’ , explode ( ‘|’ , wordwrap ( substr ( $color_hex , 1 ), 2 , ‘|’ , 1 )));
print_r ( getRgbFromGd ( $var ));
// Output: Array ( [0] => 255 [1] => 255 [2] => 255 )
If you happen to need a way to output a Windows BMP file (e.g. when using the PEAR ExcelWriter), feel free to use the following code:
function imagebmp ( $im , $fn = false )
if (! $im ) return false ;
if ( $fn === false ) $fn = ‘php://output’ ;
$f = fopen ( $fn , «w» );
if (! $f ) return false ;
//Image dimensions
$biWidth = imagesx ( $im );
$biHeight = imagesy ( $im );
$biBPLine = $biWidth * 3 ;
$biStride = ( $biBPLine + 3 ) & ~ 3 ;
$biSizeImage = $biStride * $biHeight ;
$bfOffBits = 54 ;
$bfSize = $bfOffBits + $biSizeImage ;
//BITMAPFILEHEADER
fwrite ( $f , ‘BM’ , 2 );
fwrite ( $f , pack ( ‘VvvV’ , $bfSize , 0 , 0 , $bfOffBits ));
//BITMAPINFO (BITMAPINFOHEADER)
fwrite ( $f , pack ( ‘VVVvvVVVVVV’ , 40 , $biWidth , $biHeight , 1 , 24 , 0 , $biSizeImage , 0 , 0 , 0 , 0 ));
$numpad = $biStride — $biBPLine ;
for ( $y = $biHeight — 1 ; $y >= 0 ; — $y )
for ( $x = 0 ; $x < $biWidth ; ++ $x )
$col = imagecolorat ( $im , $x , $y );
fwrite ( $f , pack ( ‘V’ , $col ), 3 );
>
for ( $i = 0 ; $i < $numpad ; ++ $i )
fwrite ( $f , pack ( ‘C’ , 0 ));
>
fclose ( $f );
return true ;
>
?>
It works the same way as regular imagejpeg/imagepng do and only supports GD2.0 true colour bitmaps (which is what’s required by ExcelWriter).
I wrote a simple function to convert an image resource to PGM (portable graymap) in order to feed it to an OCR program. It works just like the rest of the image output functions, and will convert to grayscale for you:
function imagepgm ( $image , $filename = null )
$pgm = «P5 » . imagesx ( $image ). » » . imagesy ( $image ). » 255\n» ;
for( $y = 0 ; $y < imagesy ( $image ); $y ++)
for( $x = 0 ; $x < imagesx ( $image ); $x ++)
$colors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , $y ));
$pgm .= chr ( 0.3 * $colors [ «red» ] + 0.59 * $colors [ «green» ] + 0.11 * $colors [ «blue» ]);
>
>
if( $filename != null )
$fp = fopen ( $filename , «w» );
fwrite ( $fp , $pgm );
fclose ( $fp );
>
else
return $pgm ;
>
>
?>
I know this might look somewhat superfluous to others, but i once came across a situation where i needed a *strong* blur on an image without having ImageMagick installed. Executing the convolution-filter several times on the same image is awfully slow and still doesn’t give a good blur.
The function below accepts a truecolor-image and a blur-factor between 0.0 and 1.0. Beware: It’s still quite slow.
function blurImage ( $srcimg , $blur )
$blur = $blur * $blur ;
$blur = max ( 0 , min ( 1 , $blur ));
$srcw = imagesx ( $srcimg );
$srch = imagesy ( $srcimg );
$dstimg = imagecreatetruecolor ( $srcw , $srch );
$cr = 0 ; $cg = 0 ; $cb = 0 ;
$nr = 0 ; $ng = 0 ; $nb = 0 ;
$rgb = imagecolorat ( $srcimg , 0 , 0 );
$or = ( $rgb >> 16 ) & 0xFF ;
$og = ( $rgb >> 8 ) & 0xFF ;
$ob = ( $rgb ) & 0xFF ;
$nr = ( $cr * $f1a ) + ( $or * $f1b );
$ng = ( $cg * $f1a ) + ( $og * $f1b );
$nb = ( $cb * $f1a ) + ( $ob * $f1b );
$rgb = imagecolorat ( $srcimg , 0 , $y );
$or = ( $rgb >> 16 ) & 0xFF ;
$og = ( $rgb >> 8 ) & 0xFF ;
$ob = ( $rgb ) & 0xFF ;
$x = $srcw ;
while ( $x —)
//horizontal
$rgb = imagecolorat ( $srcimg , $x , $y );
$cr = ( $rgb >> 16 ) & 0xFF ;
$cg = ( $rgb >> 8 ) & 0xFF ;
$cb = ( $rgb ) & 0xFF ;
$nr = ( $cr * $f1a ) + ( $or * $f1b );
$ng = ( $cg * $f1a ) + ( $og * $f1b );
$nb = ( $cb * $f1a ) + ( $ob * $f1b );
//vertical
$rgb = imagecolorat ( $dstimg , $x , $y + 1 );
$vr = ( $rgb >> 16 ) & 0xFF ;
$vg = ( $rgb >> 8 ) & 0xFF ;
$vb = ( $rgb ) & 0xFF ;
$nr = ( $nr * $f1a ) + ( $vr * $f1b );
$ng = ( $ng * $f1a ) + ( $vg * $f1b );
$nb = ( $nb * $f1a ) + ( $vb * $f1b );
$srcimg = imagecreatefromjpeg ( «test.jpg» );
$dstimg = blurImage ( $srcimg , 0.2 );
header ( ‘Content-type: image/jpeg’ );
echo( imagejpeg ( $dstimg ) );
exit();
This is an example of get high resolution images.
/**
* Class name : resizeImage
* Created by : wang
* Description : This class is to resize the image from original size to new size
*/
class resizeImage
/**
* Function name : resize_img
* Description : This function is to resize image
* @param : $origimg variable is the original image
* @param : $newimg variable is the new image
* @param : $w variable is the width of image
* @param : $f variable is the height of image
*/
public function resize_img ( $origimg , $newimg , $w , $h ) $info = getimagesize ( $origimg );
$mime = $info [ ‘mime’ ];
// Make sure that the requested file is actually an image
if( substr ( $mime , 0 , 6 ) != ‘image/’ )
header ( ‘HTTP/1.1 400 Bad Request’ );
return ‘Error: requested file is not an accepted type: ‘ . $origimg ;
exit();
>
// Check they extention of image
$extension = image_type_to_extension ( $info [ 2 ]);
if( strtolower ( $extension ) == ‘.png’ ) $img = $this -> resize_imagepng ( $origimg , $w , $h );
imagepng ( $img , $newimg );
imagedestroy ( $img );
>elseif( strtolower ( $extension ) == ‘.jpeg’ ) $img = $this -> resize_imagejpeg ( $origimg , $w , $h );
imagejpeg ( $img , $newimg );
imagedestroy ( $img );
>elseif( strtolower ( $extension == ‘.gif’ )) $img = $this -> resize_imagegif ( $origimg , $w , $h );
imagegif ( $img , $newimg );
imagedestroy ( $img );
>
>
/**
* End function name : resize_img
*/
/**
* Function name : resize_imagepng
* Description : This function is to resize png image
* @param : $file variable is the original image
* @param : $w variable is the width of image
* @param : $f variable is the height of image
*/
private function resize_imagepng ( $file , $w , $h ) list( $width , $height ) = getimagesize ( $file );
$src = imagecreatefrompng ( $file );
$dst = imagecreatetruecolor ( $w , $h );
imagecopyresampled ( $dst , $src , 0 , 0 , 0 , 0 , $w , $h , $width , $height );
return $dst ;
>
/**
* End function name : resize_imagepng
*/
/**
* Function name : resize_imagejpeg
* Description : This function is to resize jpeg image
* @param : $file variable is the original image
* @param : $w variable is the width of image
* @param : $f variable is the height of image
*/
private function resize_imagejpeg ( $file , $w , $h ) list( $width , $height ) = getimagesize ( $file );
$src = imagecreatefromjpeg ( $file );
$dst = imagecreatetruecolor ( $w , $h );
imagecopyresampled ( $dst , $src , 0 , 0 , 0 , 0 , $w , $h , $width , $height );
return $dst ;
>
/**
* End function name : resize_imagejpeg
*/
/**
* Function name : resize_imagegif
* Description : This function is to resize gif image
* @param : $file variable is the original image
* @param : $w variable is the width of image
* @param : $f variable is the height of image
*/
private function resize_imagegif ( $file , $w , $h ) list( $width , $height ) = getimagesize ( $file );
$src = imagecreatefromgif ( $file );
$dst = imagecreatetruecolor ( $w , $h );
imagecopyresampled ( $dst , $src , 0 , 0 , 0 , 0 , $w , $h , $width , $height );
return $dst ;
>
/**
* End function name : resize_imagegif
*/
>
/**
* End class name : resizeImage
*/
?>
Imagick::resizeImage
Scales an image to the desired dimensions with a filter.
Note: The behavior of the parameter bestfit changed in Imagick 3.0.0. Before this version given dimensions 400×400 an image of dimensions 200×150 would be left untouched. In Imagick 3.0.0 and later the image would be scaled up to size 400×300 as this is the «best fit» for the given dimensions. If bestfit parameter is used both width and height must be given.
Parameters
The blur factor where > 1 is blurry, < 1 is sharp.
Return Values
Returns true on success.
Changelog
Version | Description |
---|---|
PECL imagick 2.1.0 | Added optional fit parameter. This method now supports proportional scaling. Pass zero as either parameter for proportional scaling. |
Examples
Example #1 Imagick::resizeImage()
function resizeImage ( $imagePath , $width , $height , $filterType , $blur , $bestFit , $cropZoom ) //The blur factor where > 1 is blurry, < 1 is sharp.
$imagick = new \ Imagick ( realpath ( $imagePath ));
?php
$imagick -> resizeImage ( $width , $height , $filterType , $blur , $bestFit );
$cropWidth = $imagick -> getImageWidth ();
$cropHeight = $imagick -> getImageHeight ();
if ( $cropZoom ) $newWidth = $cropWidth / 2 ;
$newHeight = $cropHeight / 2 ;
$imagick -> cropimage (
$newWidth ,
$newHeight ,
( $cropWidth — $newWidth ) / 2 ,
( $cropHeight — $newHeight ) / 2
);
$imagick -> scaleimage (
$imagick -> getImageWidth () * 4 ,
$imagick -> getImageHeight () * 4
);
>
header ( «Content-Type: image/jpg» );
echo $imagick -> getImageBlob ();
>
User Contributed Notes 6 notes
Having to do alot of resizing, i needed to know the speeds of the different resize filters.
This was how long it took to resize a 5906×5906 JPEG image to 1181×1181.
FILTER_POINT took: 0.334532976151 seconds
FILTER_BOX took: 0.777871131897 seconds
FILTER_TRIANGLE took: 1.3695909977 seconds
FILTER_HERMITE took: 1.35866093636 seconds
FILTER_HANNING took: 4.88722896576 seconds
FILTER_HAMMING took: 4.88665103912 seconds
FILTER_BLACKMAN took: 4.89026689529 seconds
FILTER_GAUSSIAN took: 1.93553304672 seconds
FILTER_QUADRATIC took: 1.93322920799 seconds
FILTER_CUBIC took: 2.58396601677 seconds
FILTER_CATROM took: 2.58508896828 seconds
FILTER_MITCHELL took: 2.58368492126 seconds
FILTER_LANCZOS took: 3.74232912064 seconds
FILTER_BESSEL took: 4.03305602074 seconds
FILTER_SINC took: 4.90098690987 seconds
I ended up choosing CATROM as it has a very similar result to LANCZOS, but is significantly faster.
Some size, image and filter and blur combinations causes artifacts or even make image completely scrambled. As far, as I see, it happens with blur values smaller than 0.25 (sometimes less) and goes worse to the point of 0 — black image. Sometimes only some values gives artifacts, like Hanning with my test image: 0.0 blur is fine, 0.1 produces artefacts.
Affected filters are e.g. Catrom, sinc, cubic, quadratic, while unaffected are e.g. Lanczos and Hanning. The problem seems to be the domain of the filter algorithms, not IMagick implementation. This image shows three filters: http://i.imgur.com/HcdwoUS.jpg
Sometimes test image could look fine, but other not, so if you are using affected filters, the 0.5 value should be safe.
This test script takes example image (you can download and use http://i.imgur.com/KsTJpFr.jpg which is affected) and creates resized images in the same directory for every filter and one of four blur values, with naming like «test.jpg.sinc.0.1.jpg» for «test.jpg» input.
$imgPath = ‘imgtest/test.jpg’ ; // set your image file
$testBlurs = [ 0 , 0.1 , 0.2 , 0.5 ]; // test these blur values
$im = new IMagick ();
$im -> readImage ( $imgPath );
foreach ((new ReflectionClass ( ‘IMagick’ ))-> getConstants () as $n => $f ) if ( strncmp ( $n , ‘FILTER_’ , 7 ) === 0 ) < // get available IMagick filters
$filterName = strtolower ( substr ( $n , 7 )); // extract filter name from constant
foreach ( $testBlurs as $blur ) $imSize = clone $im ;
$imSize -> resizeImage ( 500 , 500 , $f , $blur , true );
$imSize -> writeImage ( sprintf ( «%s-%s-%.1f.jpg» , $imgPath , $filterName , $blur ));
$imSize -> destroy ();
>
>
>