Home > Blockchain >  Issues with image quality when converting image to php GDImage object
Issues with image quality when converting image to php GDImage object

Time:03-03

I'm currently trying to convert an image to PHP's gdImage object so that I can crop and resize it before displaying it on a site.

This is a simplified version of the php that to demo the issue.

$src_image = imagecreatefromjpeg('./original.jpg');
imagejpeg($src_image, './copy.jpg', 100);

which just converts to a php object and re writes it to a file

This is an example image that's losing it's quality https://s4.chorus-mk.thirdlight.com/file/24/VqvaOZDVqqaDrJ_VqtRlVzq4gcw/width=1140/height=1140/format=JPG/fit=stretch/crop=1228x0 4912x4912/rev=1/EV2021BPCP_RM_Members Bar_0112.jpg

Can anyone suggest a reason why this is happening?

CodePudding user response:

Your image contains an ICC colour profile. You can see that with ImageMagick using:

magick identify -verbose YOURIMAGE

or with exiftool like this:

exiftool YOURIMAGE

ExifTool Version Number         : 12.30
File Name                       : EV2021BPCP_RM_Members Bar_0112.jpg
Directory                       : .
File Size                       : 296 KiB
File Modification Date/Time     : 2022:03:02 15:47:47 00:00
File Access Date/Time           : 2022:03:02 15:50:03 00:00
File Inode Change Date/Time     : 2022:03:02 15:49:37 00:00
File Permissions                : -rw-r--r--
File Type                       : JPEG
File Type Extension             : jpg
MIME Type                       : image/jpeg
JFIF Version                    : 1.01
Resolution Unit                 : inches
X Resolution                    : 300
Y Resolution                    : 300
Profile CMM Type                : Little CMS
Profile Version                 : 2.3.0
Profile Class                   : Display Device Profile
Color Space Data                : RGB
Profile Connection Space        : XYZ
Profile Date Time               : 2008:01:10 15:30:40
Profile File Signature          : acsp
Primary Platform                : Microsoft Corporation
CMM Flags                       : Not Embedded, Independent
Device Manufacturer             : Little CMS
Device Model                    : 
Device Attributes               : Reflective, Glossy, Positive, Color
Rendering Intent                : Perceptual
Connection Space Illuminant     : 0.9642 1 0.82491
Profile Creator                 : Little CMS
Profile ID                      : 0
Device Mfg Desc                 : little cms
Profile Description             : ProPhoto
Device Model Desc               : ProPhoto
Media White Point               : 0.96396 1 0.82414
Red Matrix Column               : 0.79752 0.28799 2e-05
Blue Matrix Column              : 0.03143 0.00012 0.82494
Green Matrix Column             : 0.13525 0.71188 -3e-05
Red Tone Reproduction Curve     : (Binary data 14 bytes, use -b option to extract)
Green Tone Reproduction Curve   : (Binary data 14 bytes, use -b option to extract)
Blue Tone Reproduction Curve    : (Binary data 14 bytes, use -b option to extract)
Chromaticity Channels           : 3
Chromaticity Colorant           : Unknown (0)
Chromaticity Channel 1          : 0.7347 0.2653
Chromaticity Channel 2          : 0.15961 0.84039
Chromaticity Channel 3          : 0.03661 0.00011
Profile Copyright               : Copyright (c) 2008, Sandy McGuffog. Some rights reserved..This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
Media Black Point               : 0 0 0
Image Width                     : 1140
Image Height                    : 1140
Encoding Process                : Baseline DCT, Huffman coding
Bits Per Sample                 : 8
Color Components                : 3
Y Cb Cr Sub Sampling            : YCbCr4:2:0 (2 2)
Image Size                      : 1140x1140
Megapixels                      : 1.3

I recommend you use a library that will preserve the profile for you, such as ImageMagick or vips.

  • Related