/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] imagecreatefromjpeg ruins quality of images

Hello,

I’ve been using imagecreatefromjpeg to regenerate an existing image. However, I do not want the quality to be changed during this process.

This is what I currently have:

[code=php]
$imagepath = http://site.com/path/to/image.jpg;
$image = imagecreatefromjpeg($imagepath);
header(‘Content-Type: image/jpeg’);
imagejpeg($image);
[/code]

Which, as you can see, is a basic use of imagecreatefromjpeg. However, the quality is greatly diminished after this process. Is there a way to keep the quality at the same level as the original (100%)? Or is there an alternative function I can use on existing images, not to resize or anything?

I’ve been googling for hours over this and haven’t been able to find any solid answers, except a hint that “imagejpeg” might be a possibility, but from what I’ve found it’s not for existing images…?

I would really appreciate any help I can get.
Thanks!

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscMay 22.2010 — imagejpeg($image,$imagepath,100)

Default for the third parameter (quality) is approx. 75, according to php.net:

http://ca2.php.net/imagejpeg

By changing it to 100, you should get a higher quality.
Copy linkTweet thisAlerts:
@boxofmochiauthorMay 22.2010 — Thank you so much for your response!

Actually I tried that code, and found it worked when I changed $imagepath to NULL:
[code=php]
$imagepath = http://site.com/path/to/image.jpg;
$image = imagecreatefromjpeg($imagepath);
header('Content-Type: image/jpeg');
imagejpeg($image, NULL, 100);
[/code]


However, the odd thing is that now the script is [I]increasing[/I] my image sizes even though the original files were optimized at smaller filesizes.

Ex: image.jpg is originally at 125kb, but after running through the script it becomes 600kb.

How is this possible?
Copy linkTweet thisAlerts:
@boxofmochiauthorMay 24.2010 — Filesize is not necessarily proportional to Quality of the file ?.

Thanks for all your help!
Copy linkTweet thisAlerts:
@tirnaMay 24.2010 — In Photoshop, and I assume other applications, the quaility property refers to how much jpeg compression you want to apply to the saved or created output image.

0% quality means maximum compression and 100% quality means minimum compression.

The more compression you apply, the smaller the output filesize will be relative to the original uncompressed filesize.
Copy linkTweet thisAlerts:
@boxofmochiauthorMay 24.2010 — Right, but the thread refers to compression by php code outputting an image.
Copy linkTweet thisAlerts:
@tirnaMay 24.2010 — But does the php function use the same or similar jpeg compression algorithm as Photoshop does? [B]I don't know.[/B]

The point I was making is that filesize (in at least Photoshop) is related to the 'quality/compression' you choose when saving a jpeg file.
×

Success!

Help @boxofmochi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...