Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save image as jpg or png using timthumb.php resizing API

Tags:

php

image

I am using popular timthumb.php to resize images. i am successfully able to change height and width of image but whenever i do "save as" by default it saves as php..

for example, if you try to save the image present at http://www.binarymoon.co.uk/demo/timthumb-basic/timthumb.php?src=castle1.jpg&w=400 it saves as a file with extension php..

how do i force to save it as png/jpg? i know the mime type is image/jpg or image/png .... all i want is the image should save as png or jpg by default... kindly help

like image 385
Chethan Thimmappa Avatar asked Dec 07 '25 20:12

Chethan Thimmappa


1 Answers

Timthumb doesn't have "save as" option (see this reply http://o7.no/biJTYD on the developer blog http://www.binarymoon.co.uk/2010/11/timthumb-hints-tips/). Timthumbs saves the processed image with the same format as the source image.

I succeded in forcing a "save as jpg" when source is "png" with the followingwork around (near line 742). You may arrange other conversions working on the if-then-else chain.

if(preg_match('/^image\/(?:jpg|jpeg)$/i', $mimeType)){ 
        $imgType = 'jpg';
        imagejpeg($canvas, $tempfile, $quality); 
} else if(preg_match('/^image\/png$/i', $mimeType)){ 
        // ***************WORKAROUND STARTS HERE****************
        //$imgType = 'png';
        //imagepng($canvas, $tempfile, floor($quality * 0.09));
        $imgType = 'jpg';
        imagejpeg($canvas, $tempfile, $quality); 
like image 132
Kurt UXD Avatar answered Dec 09 '25 13:12

Kurt UXD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!