Transparency with GifCreator php class not working - php

I try to use the Gifcreator php class to create an animated GIF image from 10 PNG images.
The animated image is created but transparency of original images is lost. I have a black bakground.
The documentation says that the transparency is determinated by first loaded image. The 10 images have a transparent background. I loaded htem in Paint whicj says that it is based on white.
Has anoyone a solution to this ?
Thanks,
The class is here : https://github.com/Sybio/GifCreator
My script here : http://www.egloff.eu/rsmaptest/slideshow.php
The first image here : http://www.egloff.eu/rsmaptest/images/image0.png
The code :
<?php
// Include the class
require_once('./testcreator/GifCreator.php');
// Instanciate the class (uses default options with the addition of width/height specified)
$gif = new GifCreator(0, 2, array(0, 0, 0),550,550);
// Add each frame to the animation
$gif->addFrame(file_get_contents('images/image9.png'), 100, true);
$gif->addFrame(file_get_contents('images/image8.png'), 100, true);
$gif->addFrame(file_get_contents('images/image7.png'), 100, true);
$gif->addFrame(file_get_contents('images/image6.png'), 100, true);
$gif->addFrame(file_get_contents('images/image5.png'), 100, true);
$gif->addFrame(file_get_contents('images/image4.png'), 100, true);
$gif->addFrame(file_get_contents('images/image3.png'), 100, true);
$gif->addFrame(file_get_contents('images/image2.png'), 100, true);
$gif->addFrame(file_get_contents('images/image1.png'), 100, true);
$gif->addFrame(file_get_contents('images/image0.png'), 500, true);
// Output the animated gif
header('Content-type: image/gif');
echo $gif->getAnimation();
?>

I found an answer to my own question, and it might help some others using the same library or other libraries based on the same original work by László Zsidi.
In the class, you have to replace the following part that appears in one or another place in 2 lines :
$Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 0 ) .
by this one
$Locals_ext = "!\xF9\x04" . chr ((( $this->DIS << 2 )) | 1 + 0 ) .
That solved my problem and transparency works now OK. i tried in 3 differents classes i've found are all based on the same work.
I hope this can help.

Related

ImageMagick Perspective distortion draws shadow, how to prevent it

i have a problem with ImageMagick (PHP version), when i make perspective distortion for a photo, for some new posints (sharp shapes) it draws gray shadows on the result image, i think it's like 3D Shadows of the object, but i can not prevent it from coming in !
here is my code:
$controlPoints= array(
$oldPoints[0] , $oldPoints[1] , $newPoints[0] , $newPoints[1],
$oldPoints[2] , $oldPoints[3] , $newPoints[2] , $newPoints[3],
$oldPoints[4] , $oldPoints[5] , $newPoints[4] , $newPoints[5],
$oldPoints[6] , $oldPoints[7] , $newPoints[6] , $newPoints[7]);
$imagick = new imagick();
$imagick->readimageblob($data);
$imagick->setImageMatte(true);
$imagick->setImageVirtualPixelMethod(\Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$imagick->distortImage( Imagick::DISTORTION_PERSPECTIVE, $controlPoints, TRUE );
could you please help me, is there any suggestions ?
I tried to remove this gray color from the image result, but it's not good solution if the original photo has the same color in it.
Regards
Hani

How to rotate full page with tcpdf?

I am using TCPDF library to write a custom size label with background image and multiple text blocks on it.
The user when sees the preview on screen of the PDF it should show in horizontal, but for printing, I need the full page rotated -90 degrees.
How can I just rotate the whole page for printing version without having to move anything?
Basically:
In my case I've already had to use a new document format for the special sizes my document required.
So I've duplicated that format, created one for Landscape and one for Portrait.
Then based on the $preview variable, if previewing I'm rendering the normal landscape document, but if not previewing, I'm using the Portrait format and orientation and also starting the transformation and rotating everything on page.
Hope this helps someone I've found no other "quick" way to accomplish this kind of full-page rotation.
<?php
// #1 get the preview attribute from
// the form that was submitted from the user
$preview= isset($_POST['preview'])?(int)$_POST['preview']:0;
// load TCPDF for CodeIgniter as a library
$this->load->library('Pdf');
// #2 set default orientation and format
$orientation='L';
$format='MAKE-L';
// #3 if not previewing, switch orientation and format to portrait
if (!$preview) {
$orientation='P';
$format='MAKE-P';
}
// create new pdf object
// (same as doing new TCPDF(), it is just the CodeIgniter wrapper)
$pdf = new Pdf($orientation, 'mm', $format, true, 'UTF-8', false);
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(0, 0, 0);
$pdf->AddPage($orientation, $format);
// #4 if not previewing, start transformation
// and rotate everything before inserting any content
if (!$preview) {
// Start Transformation
$pdf->StartTransform();
// Rotate counter-clockwise centered by (x,y)
$pdf->Rotate(-90, 70, 70); // <-- TODO: test this very well because 70 and 70 was just guessing, there is no math behind that so not sure if it will work always
}
// put your content here,
// for example set font and add a text
$pdf->SetFont('times', '', 7, '', true);
$pdf->writeHTMLCell(0, 0, 25.4, 2, 'lot number', 0, 1, 0, true, '', true);
/// end content
// #5 if not in preview mode, finish the transformation
if (!$preview) {
// Stop Transformation
$pdf->StopTransform();
}
$pdf->Output('example.pdf', 'I');
/**
* Last but very important note:
* I have added my formats in tcpdf/includes/tcpdf_static.php file.
* >> MAKE-L for Landscape
* >> MAKE-P for Portrait
*/
public static $page_formats = array(
// Make
'MAKE-L' => array( 396.850, 425.196), // = ( h 140 x w 150 ) mm
// Make
'MAKE-P' => array( 425.196, 396.850 ), // = ( h 140 x w 150 ) mm
// .. rest of formats here ../
);
The setPageFormat() method should do the job. You also can pass the parameter to the $format parameter of AddPage():
$pdf->AddPage($orientation, ['format' => $format, 'Rotate' => -90]);

phpImageWorkshop: Removing a layer after saving

In the phpImageWorkshop documentation (http://phpimageworkshop.com/doc/13/saving.html) it says:
...after saving, you'll be able to continue to use your document and
to perform some actions on its sublayers, really convenient !
However, after calling save() I'm unable to remove the watermark layer.
I start by loading the photo and watermark and resize the photo:
$photo = PHPImageWorkshop\ImageWorkshop::initFromPath($tmp_name);
$mark = PHPImageWorkshop\ImageWorkshop::initFromPath($watermark);
$photo->resizeInPixel(960, null, true);
And then I add the watermark, save the photo, then remove the watermark (so I can make other sizes without a watermark without creating a new object):
$photo->addLayer(1, $mark, 0, 0, 'LB');
$photo->save($path, $filename, false, null, 80); // file correctly has watermark
$photo->remove(1);
$photo->resizeInPixel(550, null, true);
$photo->save($path, $filename, false, null, 80); // file has watermark, not correct
This does not delete the watermark layer. However, if I call remove() before save() it will remove the watermark:
$photo->addLayer(1, $mark, 0, 0, 'LB');
$photo->remove(1); // calling remove() before save removes watermark
$photo->save($path, $filename, false, null, 80); // file does not have watermark
I cannot understand why this is happening, since the documentation clearly says calling save() does not change the layers.
I've confirmed that the watermark layer is being put on layer level 1, and it works OK if I do not call save().
Despite the documentation saying you'll be able to continue to use your document, the fact is that the save() function calls getResult() which returns a merged resource image (this is in ImageWorkshopLayer.php)
However, if you create a 'base layer' and add the photo and watermark on top of it, the save() function appears to merge to the base layer - leaving the photo and mark untouched, so you can remove the mark and re-save (which causes the photo to be re-merged onto the base layer) i.e.
$baseimg = PHPImageWorkshop\ImageWorkshop::initVirginLayer(1024,800);
$photo = PHPImageWorkshop\ImageWorkshop::initFromPath("test.png");
$mark = PHPImageWorkshop\ImageWorkshop::initFromPath("test2.png");
$iphoto= $baseimg->addLayerOnTop($photo, 0, 0, 'LB');
$imark= $baseimg->addLayerOnTop($mark, 0, 0, 'LB');
$baseimg->resizeInPixel(960, null, true);
// file correctly has watermark
$baseimg->save(__DIR__, "test_out.png", false, null, 80);
$baseimg->remove($imark['id']);
// file correctly has no watermark
$baseimg->save(__DIR__, "test_out2.png", false, null, 80);
Note that I use the return value from addLayerOnTop() to determine the 'id' pf the layer to remove.
EDIT: although the above works, it is not ideal because you really need the resulting image to be the size of the re-sized photo. Also I found that once the photo is made a layer, you have to resize that layer (not the original photo) so...
// load photo and mark
$photo = PHPImageWorkshop\ImageWorkshop::initFromPath("test.png");
$mark = PHPImageWorkshop\ImageWorkshop::initFromPath("test2.png");
// resize the photo 1st time
$photo->resizeInPixel(960,null, true);
$width= $photo->getWidth();
$height= $photo->getHeight();
// make empty base image same size as photo
$baseimg = PHPImageWorkshop\ImageWorkshop::initVirginLayer($width,$height);
// add photo and mark onto base image
$iphoto= $baseimg->addLayerOnTop($photo, 0, 0, 'LT');
$imark= $baseimg->addLayerOnTop($mark, 0, 0, 'LT');
$photoid= $iphoto['id']; // get layer id's
$markid= $imark['id'];
// file correctly has watermark
$baseimg->save(__DIR__, "test_out.png", false, null, 80);
// remove mark layer
$baseimg->remove($imark['id']);
// resize photo again
// - photo is now a layer in baseimg
$baseimg->layers[$photoid]->resizeInPixel(550,null, true);
$width= $baseimg->layers[$photoid]->getWidth();
$height= $baseimg->layers[$photoid]->getHeight();
// crop baseimg to match photo size
$baseimg->cropInPixel($width,$height);
// file correctly has no watermark
$baseimg->save(__DIR__, "test_out2.png", false, null, 80);
That seems to work fine now.

Drop shadow on text

I am looking to add drop shadow to text on an image using PHP.
I am aware on how to add text to images and how some libraries allow you to add block shadowing, but I cannot see any which allow you to add a faded drop shadow.
Is this possible?
What you want is Imagick::shadowImage ( float $opacity , float $sigma , int $x , int $y )
Here's an example where I put a drop shadow on some text and then superimpose that on a background image...
$background_layer = new Imagick('poster_pic_01.jpg'); # background image
$text_layer = new Imagick('transparent400.png'); # empty transparent png of the same size
$text_layer->annotateImage( $ImagickDraw, $pad_left, $pad_top, 0, "Your text here" );
/* create drop shadow on it's own layer */
$shadow_layer = $text_layer->clone();
$shadow_layer->setImageBackgroundColor( new ImagickPixel( 'black' ) );
$shadow_layer->shadowImage( 75, 5, 5, 5 );
/* composite original text_layer onto shadow_layer */
$shadow_layer->compositeImage( $text_layer, Imagick::COMPOSITE_OVER, 0, 0 );
/* composite shadow_layer (which now has text AND the shadow) onto image_layer */
$background_layer->compositeImage( $shadow_layer, Imagick::COMPOSITE_OVER, 0, 0 );
Hope this helps,
Roger
GD can't do this out of the box. If you can, use ImageMagick. Examples on how to do shaped shadows here.

tcpdf imagebox and cropping

I would like to add a 'imagebox' a box which contains the image and crops exceeding image value that is outside of this box. something like this:
I am not sure on how to do this if it's even possible.
Actually you can do this with Clipping.
The below line would show a photo of 200X300:
$pdf->Image('photo.JPG', 100, 100, 200, 300, '', true, '', false, 300);
To clip it you need:
$pdf->StartTransform();
$pdf->Rect(100, 100, 200, 300, 'CNZ'); //Clipping mask (CNZ style makes your day)
$pdf->Image('photo.JPG', 50, 50, 300, 400, '', true, '', false, 300);
//this would actually cut off a 50 units a in each direction.
$pdf->StopTransform();
You could crop an image with php , store it as a temp_file pass it to tcpfd and then delete it after the rendering of the pdf was done . Another option would be to use html/css to position a html element over the image but as we all know tcpdf doesn't know too much about css so i don't know if it will work .

Categories