ilovepdf / ilovepdf-php

iLovePDF Rest Api - PHP Library (https://developer.ilovepdf.com)
146 stars 39 forks source link

Add image to Watermark is not working #8

Closed divsbhalala closed 6 years ago

divsbhalala commented 6 years ago

Add image to Watermark is not working getting null results

Here is the ILovePDFwatermark object Ilovepdf\WatermarkTask Object ( [mode] => [text] => [image] => /var/www/tweak.com/htdocs/wp-content/uploads/2017/10/A-letter2-11.png [pages] => 1-3 [vertical_position] => bottom [verticalPositionValues:Ilovepdf\WatermarkTask:private] => Array ( [0] => bottom [1] => middle [2] => top )

[horizontal_position] => left
[horizontalPositionValues:Ilovepdf\WatermarkTask:private] => Array
    (
        [0] => left
        [1] => center
        [2] => right
    )

[vertical_position_adjustment] => 
[horizontal_position_adjustment] => 
[mosaic] => 1
[rotation] => 0
[font_family] => 
[fontFamilyValues:Ilovepdf\WatermarkTask:private] => Array
    (
        [0] => Arial
        [1] => Arial Unicode MS
        [2] => Verdana
        [3] => Courier
        [4] => Times New Roman
        [5] => Comic Sans MS
        [6] => WenQuanYi Zen Hei
        [7] => Lohit Marathi
    )

[font_style] => 
[font_size] => 
[font_color] => 
[transparency] => 80
[layer] => above
[layerValues:Ilovepdf\WatermarkTask:private] => Array
    (
        [0] => above
        [1] => below
    )

[task] => v02rjq6jhcc56ykf6856cbb3sA4fm56vhsA1zv6z8dlsbk2w7nhxgktv27l0wj8ycrgtbmbv1zwhl2qstt9ymAcsd72dvbrlA4d3v4m2wrpbcj3kgvh7grfh6rglwkv5j6tzds9bj0Alzqwkz9snj25qhq
[files] => Array
    (
        [0] => Ilovepdf\File Object
            (
                [server_filename] => 00640bcdf3fce4200eca99d4af934a7b5506cd05de3b886d915a85c2d46b8a11.pdf
                [filename] => 20539441-104.pdf
                [rotate] => 0
                [password] => 
            )

    )

[tool] => watermark
[packaged_filename] => 20539441-104_6582842_{date}_watermark_image
[output_filename] => 20539441-104_6272893_{date}_watermark_image
[ignore_errors] => 1
[ignore_password] => 1
[try_pdf_repair] => 1
[meta] => Array
    (
    )

[custom_int] => 
[custom_string] => 
[statusValues:Ilovepdf\Task:private] => Array
    (
        [0] => 
        [1] => TaskSuccess
        [2] => TaskDeleted
        [3] => TaskWaiting
        [4] => TaskProcessing
        [5] => TaskSuccessWithWarnings
        [6] => TaskError
        [7] => TaskNotFound
    )

[result] => 
[outputFile] => 
[outputFileName] => 20539441-104_6272893_{date}_watermark_image
[outputFileType] => 
[secretKey:Ilovepdf\Ilovepdf:private] => secret_key_8d27cc2a9438f34fd56b7675132b91ee_e7tUD45f6fee0d435e7254ea3e17ba54e9fb1
[publicKey:Ilovepdf\Ilovepdf:private] => project_public_e01d8d752a10309aa1d29db999ec2ba6_KIhnU309c7296019ad408ea735adcecc568e9
[workerServer:Ilovepdf\Ilovepdf:private] => https://api1.ilovepdf.com
[token] => eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJhdWQiOiIiLCJpYXQiOjE1MDkwMTM1NTcsIm5iZiI6MTUwOTAxMzU1NywiZXhwIjoxNTA5MDI3OTU3LCJqdGkiOiJwcm9qZWN0X3B1YmxpY19lMDFkOGQ3NTJhMTAzMDlhYTFkMjlkYjk5OWVjMmJhNl9LSWhuVTMwOWM3Mjk2MDE5YWQ0MDhlYTczNWFkY2VjYzU2OGU5In0.MrcymqdcrB5E9Ftcl2QNm93BMxtkVVpkd5K3ofm7KSE
[timeDelay] => 5400
[encrypted:Ilovepdf\Ilovepdf:private] => 
[encryptKey:Ilovepdf\Ilovepdf:private] => 
[timeout] => 10
[timeoutLarge] => 

)

marcogrossisas commented 6 years ago

Hello @divsbhalala , please can you paste your code to better understand where the problem can be.

Thanks,

divsbhalala commented 6 years ago

@marcogrossisas here is the code

public function addWatermarkToPdf($pdfFilePath, $watermark_type='text', $watermark_text='', $watermark_image='', $fontSize=18, $fontColor='#000',
$fontFamily='courier', $range='all', $horizontalPosition='left', $verticalPosition='bottom', $opacity=50, $page_layer_pos='bottom', $watermark_rotation=35, $rotation=35, $name, $pkjFileName){

    $watermark = new WatermarkTask( $this->publicKey, $this->secretKey );
    $fileA = $watermark->addFile($pdfFilePath);
    $fileA->setRotation($rotation);
    if($watermark_type=='text'){
        $watermark->setText($watermark_text);
        $watermark->setFontFamily($fontFamily);
        $watermark->setFontSize($fontSize);
        $watermark->setFontColor($fontColor);
    }
    else if(!empty($watermark_image)){
        $watermark->setImage($watermark_image);
    }

    $watermark->setMosaic(true);
    $watermark->setPages($range);
    $watermark->setHorizontalPosition($horizontalPosition);
    $watermark->setVerticalPosition($verticalPosition);
    $watermark->setTransparency($opacity);
    $watermark->setLayer($page_layer_pos);
    $watermark->setRotation($watermark_rotation);

    $watermark->setOutputFilename($name);
    $watermark->setPackagedFilename($pkjFileName);
    $watermark->execute();
    return $watermark;
}

and I am calling function like this

$ILovepdf_WPPlugin_Tasks->addWatermarkToPdf($upload['path'] . '/' . $fileName, $watermark_type, $watermark_text, $upload['path'] . '/' . $watermark_image, $fontSize, $fontColor, $fontFamily,
        $fromPage.'-'.$toPage, $horizontalPosition, $verticalPosition, $opacity, $page_layer_pos, $watermark_rotation, $rotation, $outputFileName, $pkjFileName);
maztch commented 6 years ago

Hello divsbhalala,

As shown in samples, you need to add the image file like you do with the pdf file, and then set the result object as a watermark image.

This should be your code when adding image:

... else if(!empty($watermark_image)){ $image = $watermark->addFile($watermark_image); $watermark->setImage($watermark_image); } ...

By the way, I have seen that you develop a wordpress plugin and I invite you to see ours at https://wordpress.org/plugins/ilovepdf/ (and code here https://github.com/ilovepdf/ilovepdf-wp), maybe will be useful, and/or save you some work...

divsbhalala commented 6 years ago

@maztch I am trying to customize your WordPress plugins as per my need for the client side.

And I am still getting null response after adding code as per your suggestions the code looks like this http://prntscr.com/h2lmgo

and here are params that I am passing http://prntscr.com/h2lmpb (Note: Don't worry About the path I am passing full path)

maztch commented 6 years ago

Sorry, my fault:

... else if(!empty($watermark_image)){ $image = $watermark->addFile($watermark_image); $watermark->setImage($image); } ...

divsbhalala commented 6 years ago

@maztch sorry but this solution is also not working

here is the code http://prntscr.com/h2ou3f and here is the request Params http://prntscr.com/h2ouij

is there any sample code? so I can see that

maztch commented 6 years ago

Is so strange because your dump files field only have the pdf file and not the image file (should contain both files). And watermark_image field should contain an id, not the filename. If it's from the code.

Add also the mode

$watermark->setMode('text') ; // or 'image'

Here you can find samples: https://github.com/ilovepdf/ilovepdf-php/tree/master/samples

Try to do a complete process for each mode (text, and image).

divsbhalala commented 6 years ago

@maztch I am already passing image file http://prntscr.com/h2pw0l

divsbhalala commented 6 years ago

I tried with both image and text mode but nothing works and in your sample there is not sample of add image to the watermark

marcogrossisas commented 6 years ago

@divsbhalala we are looking into it. Your code seems right if you added the following line to your last code sent to us

$watermark->setMode('image');

But with the code you sent us something is wrong with the request data is sending.

divsbhalala commented 6 years ago

@marcogrossisas if you want to test the result Please look at https://www.tweak.com/add-watermark/ the code is pushed here as per your suggestion and it's still not working. perhaps for the text watermark, it's working fine.

so do you want me try other solution?

marcogrossisas commented 6 years ago

@divsbhalala sorry for the late answer, here there is a complete working example with an image. The problem may be on the suggested $watermark->setImage($image); that it should have been $watermark->setImage($image->server_filename);:


//include the autoloader
require_once('../vendor/autoload.php');

use Ilovepdf\WatermarkTask;

// you can call task class directly
// to get your key pair, please visit https://developer.ilovepdf.com/user/projects
$myTask = new WatermarkTask('public','secret')

// file var keeps info about server file id, name...
// it can be used latter to cancel file
$file = $myTask->addFile('pols.pdf');

$image = $myTask->addFile('img.png');
// set mode to text
$myTask->setMode("image");

// set the text
//$myTask->setText("watermark text");
$myTask->setImage($image->server_filename);
// set pages to apply the watermark
$myTask->setPages("1-5,7");

// set vertical position
$myTask->setVerticalPosition("top");

// set horizontal position
$myTask->setHorizontalPosition("right");

// adjust vertical position
$myTask->setVerticalPositionAdjustment("100");

// adjust horizontal position
$myTask->setHorizontalPositionAdjustment("100");

// set mode to text
$myTask->setFontFamily("Arial");

// set mode to text
$myTask->setFontStyle("Italic");

// set the font size
$myTask->setFontSize("12");

// set color to red
$myTask->setFontColor("#ff0000");

// set transparency
$myTask->setTransparency("50");

// set the layer
$myTask->setLayer("below");

// and set name for output file.
// the task will set the correct file extension for you.
$myTask->setOutputFilename('watermarked');

// process files
$myTask->execute();

// and finally download the unlocked file. If no path is set, it will be downloaded on current folder
$myTask->download();

Please inform us if now you could solve it, if so, we will update the code to accept $image as object or $image->server_filename to avoid this kind of errors.

divsbhalala commented 6 years ago

@marcogrossisas thanks for your supports this solution is working $myTask->setImage($image->server_filename);

divsbhalala commented 6 years ago

Also, there is some documentation issue from your site that I have just notice. If you want to know Please let me know where I put suggestion for that