📚 Encyclopedia

Web Development · updated 2026-10-13

Resizing uploaded photos on the server (PHP GD)

imagesphpgdexifprogressive-jpegperformanceuploads

Non-technical site owners upload photos straight off their phones: 3 to 4 MB, 3000+ pixels, EXIF-rotated JPEGs. Rendered at 520 CSS pixels, those made the Hunsley Hills Salon homepage about 10 MB, and no lazy-loading or shimmer placeholder fixes that on mobile data. The fix is to normalize every image once, at upload time, on the server. The PHP GD and exif extensions are available on the Alma 9 server.

What the upload processor does

  1. Bake in EXIF orientation first. GD discards EXIF metadata, so a portrait phone photo that relies on the Orientation tag comes out sideways. Read it with exif_read_data() and rotate or flip the pixels (imagerotate, imageflip) before anything else.
  2. Cap the long edge (1600 px was chosen; the site never renders more than ~520 CSS px, so this covers 3x displays).
  3. JPEG quality 80, progressive (imageinterlace($img, true)). Progressive JPEGs paint a blurry full frame first instead of loading top-down, which looks far better on slow links.
  4. Preserve PNG alpha (imagealphablending(false) + imagesavealpha(true) on the target).
  5. Leave GIFs alone, since GD would drop the animation.
  6. Fall back to the original bytes if GD fails or the output is not smaller.

Result: a 3.5 MB 3000x2000 upload became a 1600 px progressive JPEG; the existing hero and gallery images went from 11.3 MB total to 1.1 MB after being reprocessed.

Reprocessing existing images

Images already in storage were recompressed locally with Python PIL (same rules: apply EXIF transpose, cap the edge, quality 80, progressive=True) and written back under the same object keys so no content had to change. Caveat: URLs served with an immutable cache header stay stale for returning visitors; see Linode (Akamai) Object Storage.

Validation on upload

Accept only JPEG, PNG, WebP and GIF, check the real type (not the filename), cap size (5 MB before processing), and store under a random name.

Front-end companions

See also

Linked from

Sources: hunsley memory (hhs-cms, hhs-frontend), 2026-07-08