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
gamehelp16
  • 1,101
  • 1
  • 7
  • 22
  • 1
    Post your code please. – kittycat Mar 27 '13 at 10:57
  • "always gives error" is not a proper description of your problem. Explain and show what you've tried, what errors occured and what you expect to happen. – Repox Mar 27 '13 at 11:00
  • @cryptic: later when I get to my computer – gamehelp16 Mar 27 '13 at 11:01
  • The subject doesn't have anything to do with your problem. You should reconcider it. SInce by reading it one is assumed to belive you have issues with creating watermarks and uploading images (two sepperate question I think to) but then you state that this is not the problem. Also, "it always gives error" is not informative enough. Could be everything form a locked file to an error before even writing the file or trying to open the wrong path. https://quality.mozilla.org/docs/bugzilla/starter-kit/how-to-write-a-proper-bug/ – inquam Mar 27 '13 at 11:02
  • @repox: i forgot exactly what the error says, but it says something like "the pic exceeded the maximum file size" if I'm not mistaken. I wonder why the error appears, because the pic is not big – gamehelp16 Mar 27 '13 at 11:02
  • @inquam: title has been edited :D – gamehelp16 Mar 27 '13 at 11:07
  • How do you "get" the file and add the watermark to it? Is it opened with fopen? In that case, do you close it? Do you save the "new" file with the same name on the same path? What file and when do you open and calculate the base64 on? – inquam Mar 27 '13 at 11:07
  • Edited: there's my code and the error i got – gamehelp16 Mar 27 '13 at 11:38

2 Answers2

0

You can try this-

<?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);
    }
}
?>

You can find more info on base64_encode

OR

Alternatively you can do this-

$imageContent= file_get_contents($yourImage);
$encoded = base64_encode($imageContent);
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
  • I think his problem has something to do with opening the file, adding the watermark and not being able to get the base64 after that. He should add the watermark, save the "new" image and then open thatone and calculate the base64 of that image. Right now we don't know if and when he saves the crated image and on which image or data he's trying to calculate the base64. – inquam Mar 27 '13 at 11:05
  • @inquam: so, the image that I can't calculate the base64 is a image created by PHP script – gamehelp16 Mar 27 '13 at 11:08
  • @gamehelp16: You add watermark and save the file and then try to open that file with *file_get_contents*? What does the error message say? – inquam Mar 27 '13 at 11:15
  • no, i use the `file_get_contents` to open directly the PHP file – gamehelp16 Mar 27 '13 at 11:16
  • @gamehelp16: The PHP file? What PHP file? Weren't you adding watermarks to images just now? :) – inquam Mar 27 '13 at 11:19
  • I use PHP script to give watermark, so, it's something like this: `watermark.php?img=IMAGE_URL` – gamehelp16 Mar 27 '13 at 11:20
0

Simple pseudo code idea

This should work fine. If you get any errors state what they say and where you get them.

inquam
  • 12,664
  • 15
  • 61
  • 101
  • but this means that uploading the image to imgur has no use, because for some reason i can't save the image to disk, so i host it to imgur – gamehelp16 Mar 27 '13 at 11:14
  • @gamehelp16: So you only want to add the watermark to the image in memory and then upload it without saving it? That should work, just calculate the base64 of the newly created image without saving it. If you are not permitted to save the image locally I guess the file you get error about trying to read with *get_file_contents* must be the original image? If that's the case, do you have permissions to read that file? – inquam Mar 27 '13 at 11:17
  • sorry, i don't know about file permissions for localhost, because i'm still testing the code in localhost – gamehelp16 Mar 27 '13 at 11:22