0

So, what I want is where user can upload image, a PHP script will add watermark to it,a nd the pic will be hosted on imgur using imgur API

Adding watermark to the image and hosting it to imgur is not the problem, but the problem is: to upload image to imgur, we must pass the base64 of the picture, and I can't fetch the pic that has been given watermark by PHP script using file_get_contents and fopen, it always gives error

Someone please help me with the problem so I can fetch the image, encode it to base64

That's all

UPDATE:

Just to tell you, i used the function to open the PHP file DIRECTLY without saving the pic to disk first, for some reason i can't save the image to the disk, that's why i use imgur

UPDATE AGAIN:

here's my code:

<?php
    header('Content-type: image/png');
    $image = $_GET['url'];
    $imagesize = getimagesize($image);
    $img = imagecreatetruecolor($imagesize[0], $imagesize[1] + 50);
    imagecopy($img, imagecreatefrompng($image), 0, 0, 0, 0, $imagesize[0], $imagesize[1]);
    $string = "Some wild watermarks";
    $font = "font.ttf";
    $fontSize="30";
    $textColor = imagecolorallocate($img, 255,255,255);
    $x=7;
    $y=$imagesize[1] + 42;
    imagettftext($img,$fontSize,0,$x,$y,$textColor,$font,$string);
    imagepng($img);
?>

Here's the error i got:

Using file_get_contents:

Warning: file_get_contents(watermark.php?url=roughdesign.png) [function.file-get-contents]: failed to open stream: Result too large in URL

Using fopen:

Warning: fopen(watermark.php?url=roughdesign.png) [function.fopen]: failed to open stream: Result too large in URL
Warning: filesize() [function.filesize]: stat failed for URL
Warning: fread(): supplied argument is not a valid stream resource in URL
4

2 回答 2

0

你可以试试这个-

<?php
function base64_encode_image ($filename=string,$filetype=string) {
    if ($filename) {
        $imgbinary = fread(fopen($filename, "r"), filesize($filename));
        return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
    }
}
?>

您可以在base64_encode上找到更多信息

或者

或者你可以这样做 -

$imageContent= file_get_contents($yourImage);
$encoded = base64_encode($imageContent);
于 2013-03-27T10:59:47.490 回答
0

简单的伪代码思路

这应该可以正常工作。如果您收到任何错误,请说明他们所说的内容以及您从哪里得到它们。

于 2013-03-27T11:10:29.033 回答