duck7000 / imdbGraphQLPHP

5 stars 0 forks source link

Person - Image Size #21

Closed GeorgeFive closed 10 months ago

GeorgeFive commented 10 months ago

Hey there! Had a feature request that may help in some niche cases. In Person.php, we currently have two choices for images... thumb and original. I've just run into a case that made me realize that maybe we should use something in between?

This person - https://www.imdb.com/name/nm9526605/

We can get the thumbnail, but this is too small for me.... https://m.media-amazon.com/images/M/MV5BNzQwOWE0Y2YtZGY1Yy00OWRmLWExYmUtZmNiYTU3MWM4YWE5XkEyXkFqcGdeQXVyODM5NjE4NzA@.UY98_CR15,0,67,98_AL_.jpg

Ok, that's too small, so let's get the original image.... well, this will absolutely destroy your memory if you try to handle it in PHP image functions: https://m.media-amazon.com/images/M/MV5BNzQwOWE0Y2YtZGY1Yy00OWRmLWExYmUtZmNiYTU3MWM4YWE5XkEyXkFqcGdeQXVyODM5NjE4NzA@.jpg

Maybe we could have three options... the previous two (thumb /// original), as well as a third for large? Which would give you... https://m.media-amazon.com/images/M/MV5BNzQwOWE0Y2YtZGY1Yy00OWRmLWExYmUtZmNiYTU3MWM4YWE5XkEyXkFqcGdeQXVyODM5NjE4NzA@._V1_SY931_.jpg

duck7000 commented 10 months ago

Well i never used the person class so this is new too me hah

In theory you can get any size you want, the part after @ controls image size and canvas size So CR15,0,167,198 will get the same image size but in a large canvas size: https://m.media-amazon.com/images/M/MV5BNzQwOWE0Y2YtZGY1Yy00OWRmLWExYmUtZmNiYTU3MWM4YWE5XkEyXkFqcGdeQXVyODM5NjE4NzA@.UY98_CR15,0,167,198_AL_.jpg

The last 2 values of CR controls the canvas size, i don't know what the other 2 does

Okay that is one part, now figure out how to control image size I figured it out: @.UY0398_CR15,0,398AL.jpg (UY is the image height but canvas width has to be removed and the last value of CR has to be the same as UY) https://m.media-amazon.com/images/M/MV5BNzQwOWE0Y2YtZGY1Yy00OWRmLWExYmUtZmNiYTU3MWM4YWE5XkEyXkFqcGdeQXVyODM5NjE4NzA@.UY0398_CR15,0,398_AL_.jpg So with both 398 values we can get any image size we want.

Now remains the question how to use this in our image function? We can use a parameter with the desired image height? Or use a parameter for small medium or large?

duck7000 commented 10 months ago

I made some changes to person photo like this, I'm not sure if the image width is necessary or would it be better to leave it out and let IMDb decide? In the title photo method i did added the width, in some cases the height will be adjusted

    /** Get cover photo
     * @param boolean $size (optional) small:  thumbnail (67x98, default)
     *                                 medium: image size (621x931)
     *                                 large:  image maximum size
     * @return mixed photo (string url if found, empty string otherwise)
     * @see IMDB person page / (Main page)
     */
    public function photo($size = "small")
    {
    $query = <<<EOF
query PrimaryImage(\$id: ID!) {
  name(id: \$id) {
    primaryImage {
      url
    }
  }
}
EOF;
        if ($this->main_photo === null) {
            $data = $this->graphql->query($query, "PrimaryImage", ["id" => "nm$this->imdbID"]);
            if ($data->name->primaryImage->url != null) {
                $img = str_replace('.jpg', '', $data->name->primaryImage->url);
                if ($size == "small") {
                    $this->main_photo = $img . 'UY98_CR15,0,67,98_AL_.jpg';
                }
                if ($size == "medium") {
                    $this->main_photo = $img . 'UY931_CR15,0,621,931_AL_.jpg';
                }
                if ($size == "large") {
                    $this->main_photo = $data->name->primaryImage->url;
                }
            } else {
                return $this->main_photo;
            }
        }
        return $this->main_photo;
    }
GeorgeFive commented 10 months ago

I checked out your code, and it seems to crop out some of the image on the medium setting. 1 2

I replaced this with

                if ($size == "medium") {
                    $this->main_photo = $img . '._V1_SY931_.jpg';
                }

And that seems to work fine.... this is a no crop, no canvas, ~600x900 version of the image. Might be the way to go?

duck7000 commented 10 months ago

Mm didn't notice the crop in my test but yes this is the best way to go i think.

I will upload the new person photo method with your changes

duck7000 commented 10 months ago

Upload is done

The only thing might be compression ratio, i don't know what happens if you omit this? small image i removed the width as sometimes IMDb adds white borders to get the required width.

There is no documentation about those image parameters after @ in the image URL so it is a guessing game What the difference is between SY931 and UY931 i don't know, do you?

duck7000 commented 10 months ago

I found this, it is helpful although not complete i guess

https://stackoverflow.com/questions/73089650/what-are-the-parameters-for-aws-media-amazon-image-hosting

CR means crop the image, not as i thought compression ratio.. And the difference between S and U is also clear now.

I changed the image parameters to get a better quality and a decent scaled image. Also changed this in Title

GeorgeFive commented 10 months ago

Looking good! I don't use the title image version, so I never noticed much over there.... I prefer DVD / blu-ray covers, and Amazon is far better for that. On person, I always used the original image, so I never noticed any issues there until I hit that huge original image which kept overloading PHP.

duck7000 commented 10 months ago

More and more images on IMDb are 2k, 4k and i have even see 8k images so yes PHP memory will be a problem with those. The thumbnail versions (both title and person) are now reduced to around 50kb (with quality set to 100) so that is fine and the quality is (at least for me) acceptable

So this is working like you aspect?

duck7000 commented 10 months ago

Mm i notice i did not update github to my latest version. Done now

duck7000 commented 10 months ago

let me know if the file size for medium is acceptable? I set the QL to 100 so that might be to much?

GeorgeFive commented 10 months ago

Looks perfect to me! I have no need for the high resolution images, but the option is there if anybody else needs it. Quality and resolution on medium is perfect for my use case.... thanks!

duck7000 commented 10 months ago

Glad it works for you Closing this now