How do I resize and convert an uploaded image to a PNG using GD?

前端 未结 9 1403
离开以前
离开以前 2020-12-05 16:08

I want to allow users to upload avatar-type images in a variety of formats (GIF, JPEG, and PNG at least), but to save them all as PNG database BLOBs

9条回答
  •  感情败类
    2020-12-05 16:34

    If you want to use gdlib, use gdlib 2 or higher. It has a function called imagecopyresampled(), which will interpolate pixels while resizing and look much better.

    Also, I've always heard noted around the net that storing images in the database is bad form:

    • It's slower to access than the disk
    • Your server will need to run a script to get to the image instead of simply serving a file
    • Your script now is responsible for a lot of stuff the web server used to handle:
      • Setting the proper Content-Type header
      • Setting the proper caching/timeout/E-tag headers, so clients can properly cache the image. If do not do this properly, the image serving script will be hit on every request, increasing the load on the server even more.

    The only advantage I can see is that you don't need to keep your database and image files synchronized. I would still recommend against it though.

提交回复
热议问题