PHP Snippets
image_flip
Information
(PHP Version >= 4.0.6)Functions used in this snippet: imagesx, imagesy, imagecreatetruecolor, imagecopy, imagedestroy.
Description
int image_flip( int img[, str type] )
Flips the image img to the direction of type
Snippet
<?php
function image_flip($img, $type=''){
$width = imagesx($img);
$height = imagesy($img);
$dest = imagecreatetruecolor($width, $height);
switch($type){
case '':
return $img;
break;
case 'vert':
for($i=0;$i<$height;$i++){
imagecopy($dest, $img, 0, ($height - $i - 1), 0, $i, $width, 1);
}
break;
case 'horiz':
for($i=0;$i<$width;$i++){
imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);
}
break;
case 'both':
for($i=0;$i<$width;$i++){
imagecopy($dest, $img, ($width - $i - 1), 0, $i, 0, 1, $height);
}
$buffer = imagecreatetruecolor($width, 1);
for($i=0;$i<($height/2);$i++){
imagecopy($buffer, $dest, 0, 0, 0, ($height - $i -1), $width, 1);
imagecopy($dest, $dest, 0, ($height - $i - 1), 0, $i, $width, 1);
imagecopy($dest, $buffer, 0, $i, 0, 0, $width, 1);
}
imagedestroy($buffer);
break;
}
return $dest;
}
?>
Example
image_flip Can be used in the following way:
<?php
$img = imagecreatefromjpeg('somefile.jpg'); // create an image
$img = image_flip($img, 'vert'); // flips image vertically
$img = image_flip($img, 'horiz'); // flips image horizontally
$img = image_flip($img, 'both'); // flips image both ways
header('Content-type: image/jpeg'); // send header
imagejpeg($img); // send image to browser
?>
0 Responses to image_flip