Wednesday 15 February 2012

php - Compress & save base64 image -


My app is getting web 64-encoded image-file from WebBrover. I have to save them to the client so I did:

  $ data = base64_decode ($ base64img); $ FileName = uniqid () '.jpg'; File_put_contents ($ UploadPath $ FileName, $ data); Return $ $ Filename;   

That works fine.

Now I need to compress & amp; Maximize Image 800 Width & amp; Maintaining height, aspect ratio

So I tried:

  $ data = base64_decode ($ base64img); $ FileName = uniqid () '.jpg'; File_put_contents ($ UploadPath $ FileName, $ data); Return $ $ Filename;   

that does not work (error: "Image zip () parameter 1 is expected to be processed, string given"). And of course, it compresses, but it does not change its shape.

Would it be good to save the file in / TMP, read it and resize / resize through GD?

Second part

Thanks, I know that

  $ Data = imagejpeg (imagecreatefromstring ($ data) $ Uploadpath $ $ fileName, 80);   

works

but now I need to resize the image to a maximum of 800 width and height. I have this function:

  Function Re-size and Compress Image Function ($ file, $ w, $ h, $ crop = FALSE) {list ($ width, $ height) = getimagesize ($ file); $ R = $ width / $ height; If ($ crop) {if ($ width> $ height) {$ width = ceil ($ width - ($ width * ($ r- $ w / $ h)); } And ($ height = roof ($ height - ($ height * ($ r- $ w / $ h));} $ newwidth = $ w; $ newhigh = $ h;} and {if ($ w / $ H & gt; $ r) {$ newwidth = $ h * $ r; $ newheight = $ h;} and {$ newheight = $ w / $ r; $ newwidth = $ w;}} $ Src = imagecreatefromjpeg ($ file $ Dst = imagecreatetruecolor ($ newwidth, $ newheight); Imagecopyresampled ($ dst, $ src, 0, 0, 0, 0, $ newwidth, $ newheight, $ width, $ height); $ Dst return;}   

So I thought I could:

  $ data = imagejpeg (resizeAndCompressImagefunction (imagecreatefromstring ($ data), 800,800), $ uploadpath. $ FileName, 80);   

that does not work.


second To answer the part:

  $ data = imagejpeg (resizeAndCompressImagefunction (imagecreatefromstring ($ data), 800,800), $ uploadpath. $ FileName, 80);   

$ data will only include either true or false, which were successful in showing the operation season Bytes $ uploadpath is $ FileName . If you want to return the actual bytes to $ data , then you must use a temporary output buffer:

  $ img = imagecreatefromstring ($ data); $ Img = resizeAndCompressImagefunction ($ IMG, 800, 800); Ob_start (); Imagejpeg ($ img, faucet, 80); $ Data = ob_get_clean ();    

No comments:

Post a Comment