Jump to content

Recommended Posts

  I'm having a small ordeal with a site and people uploading large images (Often over 1MB). I can't rely on client side size restrictions, so that leaves me with two choices... to recompress into JPEG before sending to the client, or compressing and resizing on upload. I know it's possible, but I don't know how (Probably a rewrite rule), but I've seen sites that when an image is loaded, it instead takes you to a page which shows the image along with copyright information, a banner, the whatnot. Though, this would make things easier, I'd rather compress the files on upload. What would be best and how would I do it?
Link to comment
https://forums.phpfreaks.com/topic/34023-dynamic-images-serverside-jpeg/
Share on other sites

  Well I'd like to be able to do a thumbnail version, the main concern is people uploading large, uncompressed files, which get used for the MySpace equivalent of a display pic. When you have 10 1.5MB pictures on a page when they are being shrunk in HTML, it kind of causes... lag? Haha.
ok, might be worth taking a look for thumbnail scripts anyway, but generally it involves [url=http://uk.php.net/gd]GD library[/url] to open, resize and display the smaller version. What its ability is to handle HUGE images is, I couldnt tell you...
yeah - generally, to keep the quality, the function you're after is [url=http://www.php.net/imagecopyresampled]imagecopyresampled[/url]. it takes box coordinates of the source, box coordinates for the destnation, and voila.
the working out comes seperately, but generally follows something like this:

[code]
<?php
$filename = "test.jpg";
$w = 200; // max width
$h = 200; // max height

//first open the file
$im = imagecreatefromjpeg($filename);

// get its width/height
$sx = imagesx($im);
$sy = imagesy($im);

// work out the scale. $w would be initially set to the max width, $h to the max height
$scale = min($w/$sx, $h/$sy);

// send the necessary headers to the browser:
header("Content-type: image/jpeg");

// scale < 1 means it needs resizing as too big
if ($scale<1)
{
// work out the new width/height
$newwidth = $scale * $sx;
$newheight = $scale * $sy;

// create a 'canvas' for it
$im2 = imagecreatetruecolor($newwidth, $newheight);

// do the copying and send to browser
imagecopyresampled($im2, $im, 0, 0, 0, 0, $newwidth, $newheight, $sx, $sy);
imagejpeg($im2);

// clean up
imagedestroy($im);
imagedestroy($im2);
}
else
{
imagejpeg($im);
imagedestroy($im);
}
?>
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.