4

One of the pages of a website i'm working on should display information about a manga such as it's cover image.

I'm trying to display an image I got by making a curl request.

<?php
    if(isset($_GET['info'])) {
        $postedData = $_GET["info"]; //json object containing info about manga such as author/title etc.
        $info = json_decode($postedData, true);
        $mangacoverid = $info['im']; //id of the cover image that i'm getting from json object

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://cdn.mangaeden.com/mangasimg/" . $mangacoverid); //link of API i'm using and adding cover id to it
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        $picture = curl_exec($ch);
        curl_close($ch);
        header('Content-type: image/jpeg');
        echo $picture; //displays image in browser
    }
        ?>

-Some 'mangacoverid' for testing purposes: ff/ff94bb880357b6b811bccbbfd3356c5ec41fbb184291323f0ed6a86a.jpg c1/c1b0173d8986681f23ecf5a69d26aa9dab4a04db4d40f99bed539198.jpg 0c/0cf6ebf78074e748ab2aeea3a0fcb9e0dd43040974c66e24fa46703f.jpg 5d/5dcfed2e033c2da62e7ac89367533ebbc344373c46a005e274a16785.png 18/18b1f0b13bccb594c6daf296c1f9b6dbd83783bb5ae63fe1716c9091.jpg 35/35bf6095212da882f5d2122fd547800ed993c58956ec39b5a2d52ad4.jpg

-While I am able to display the image in the page, the whole page background becomes black with the image in the middle of the page. (i.e. style="margin: 0px; background: #0e0e0e;>").

What I am trying to do is to insert the image in an HTML tag, so that I can place it somewhere else in the page.

I tried just putting the normal cdn link with 'mangacoverid' attached to it in an tag but the image provider doesn't allow hotlinking and it throws 403 error.

Any help really appreciated!

Zubaer Rahman
  • 95
  • 1
  • 7

2 Answers2

2

I tested your code and it seems to be working correctly when the cover ID is hard coded. Could the issue be that the JSON passed via query param is not getting parsed correctly (due to URL encoding)?

Does it work correctly for you with a cover ID hard coded?

File image.php

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf('https://cdn.mangaeden.com/mangasimg/%s', $_GET['id']));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$picture = curl_exec($ch);
curl_close($ch);
header('Content-type: image/jpeg');
echo $picture;

And in your script that generates the page displaying the image:

// Assuming $info holds the decoded json for the manga
echo(sprintf('<img src="image.php?id=%s>', $info['im']);
atymic
  • 3,093
  • 1
  • 13
  • 26
  • Thanks for the reply. The url itself worked and returned an image but I wanted to take that image and display it within an tag in my page. I solved by creating a file (temp.jpg) in the directory. I added the following code after "curl_close($ch)" and deleted the code after that: "$img = imagecreatefromstring($picture); imagejpeg($img,'temp.jpg', 100);". Afterwards I just set in the html to refer to the image in the file. – Zubaer Rahman Jul 19 '19 at 01:44
  • The problem with that is that only one image will be displayed on a page with multiple images. – atymic Jul 19 '19 at 02:02
  • You can just rename your script to "image.php" and then in the script that outputs your HTML you can do ``. – atymic Jul 19 '19 at 02:03
  • For my current purpose I just need to display one image per page (i.e.: when reading a manga I just need to display the current chapter). Could you explain what You meant in the second comment? :) – Zubaer Rahman Jul 19 '19 at 02:09
  • The problem is, if two different users load different pages at the same time, they will get the wrong images. – atymic Jul 19 '19 at 02:10
  • Damn thats a fair pont. How would I handle that with the ? Edit: alright thanks :) – Zubaer Rahman Jul 19 '19 at 02:12
  • Okay I just tried your code. It does display the image but that's the only thing displayed in the page with a black background. The tag I echoed doesn't appear in Inspect Element nor any other html I write. – Zubaer Rahman Jul 19 '19 at 02:52
  • Did you put the code to get the image in a separate file, named `image.php`? – atymic Jul 19 '19 at 02:56
  • OMG i did it. Man you're the best. The error was because there was $_GET() instead of $_GET[] in 'image.php' thus the image was not displaying. Thank you so much. – Zubaer Rahman Jul 19 '19 at 03:44
  • Oops, my bad. I've updated the answer. Glad you got it working. – atymic Jul 19 '19 at 03:45
0

Ok, I'll post the solution in case anyone ever bumps into this.

The code is the mostly the same as original: just edited a few lines as shown below.

-I created a file in the directory called 'tempcover.jpg'.

-I got the image using the function 'imagecreatefromstring($picture)'.

-I saved the image into the file with 'imagejpeg($img,'tempcover.jpg', 100)'.

By doing this I can just refer to the file in the HTML tag and display it the way I want to.

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://cdn.mangaeden.com/mangasimg/" . $mangacoverid); //link of API i'm using and adding cover id to it
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    $picture = curl_exec($ch);
    curl_close($ch);
    $img = imagecreatefromstring($picture);
    imagejpeg($img,'tempcover.jpg', 100);
    ?>

<!--HTML-->
    <img src="tempcover.jpg" alt="bruh" height="400px" width="300px">
Zubaer Rahman
  • 95
  • 1
  • 7