Imagick::setImageFormat


wizard apg

SETS the format of an image.



<?php

bool 
public Imagick::setImageFormat(string $format);

?>

$format


STRING presentation of the image format.

Format support depends on the ImageMagick installation.





This function returns a TRUE on success.



  1 EXERCISE   

<?php

$imagick 
= new Imagick();

$pngfn = PATH2IMGW . '/png/wizard.png';

$imagick->readImage($pngfn);

// Returns a large PNG
$pngData = $imagick->getImageBlob();
$imagick->setImageFormat('pdf');

// Returns pdf blob string immediately
$pdfData = $imagick->getImageBlob(); 

echo 
"PNG:<br>" . $pngData;
echo 
'<br><br><br>PDF:<br>';
echo 
$pdfData;

?>

  2 EXERCISE   

<?php

$imagick 
= new Imagick();

$pngfn = PATH2IMGW . '/png/wizard.png';

$imagick->readImage($pngfn);

$formats =  $imagick->queryFormats();

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   RUN this exercice several times...
   
   Each time it is executed a 
                   new result will be obtained.

   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$nbr = count($formats);

$rnd = mt_rand(0, $nbr - 1);

$pngData = $imagick->getImageBlob();

$imagick->setImageFormat($formats[$rnd]);

$Data = $imagick->getImageBlob(); 

echo 
"[$rnd] -> $formats[$rnd]<br><br>";

echo 
$Data;

?>

  3 EXERCISE   

<?php

$imagick 
= new Imagick();

$pngfn = PATH2IMGW . '/png/wizard.png';
$str3img = PATH2IMGL . '/results/wizard new.bmp';

$imagick->readImage($pngfn);

$pngData = $imagick->getImageBlob();
$imagick->setImageFormat('png');

$pngData = $imagick->getImageBlob(); 

echo 
$pngData . '<br><br>';

echo 
"BMP:<br><br>";

$img = imagecreatefromstring($pngData);

imagebmp($img, $str3img);

echo 
basename($str3img); ?>
<br><br>The image created by the use of this STRING:
<br><br>
<img src="<?php echo $str3img; ?>"
 alt="<?php echo $str3img; ?>" 
 title="<?php echo $str3img; ?>">

wizard new.bmp

The image created by the use of this STRING:

img/results/wizard new.bmp apr

  4 EXERCISE   

<?php

$image 
= new Imagick();
$image->newImage(1, 1, new ImagickPixel('#ffffff'));
$image->setImageFormat('png');
$pngData = $image->getImagesBlob();
echo 
strpos($pngData, "\x89PNG\r\n\x1a\n") === 0 ? 'Ok' : 'Failed'; 

?>

Ok