I know this is probably impossible, but I want to generate sprites from user-uploaded images dynamically using PHP. Since my CSS file is already dynamically generated, I can add the appropriate CSS.
Please let me know as well if it’s not possible. Thanks ?
@NogDogOct 15.2010 — #I'm sure it's probably "possible", but I'm not clear as to exactly what you want to do with the uploaded image, so I'm not sure how easy/difficult it would be.
@narutodude000authorOct 15.2010 — #My website allows users to upload thumbnails for posts. The multiple images make my website load fairly slow. If I combine them into a large image and use a CSS sprite, it will load a lot faster.
Each time a new thumbnail is uploaded, the thumbnail can be added to the large image.
@NogDogOct 16.2010 — #Here's something using a resize function I already had lying around, which hopefully will give you some ideas: [code=php] <?php // TEST: define('WIDTH', 200); define('HEIGHT', 150); $images = glob('images/*.png'); $thumb = resize(array_shift($images) , WIDTH, HEIGHT, $error) or die($error); foreach($images as $img) { $new = resize($img, WIDTH, HEIGHT, $error) or die($error); $thumb = mergeThumbs($thumb, $new, WIDTH); } header('Content-Type: image/png'); imagepng($thumb);
// FUNCTIONS:
/** * Resize image to specific dimension, cropping as needed * @return resource Resized image resource, or boolean false on failure * @param string $imgFile Path to image to be resized * @param int $width * @param int $height * @param string $error Error message */ function resize($imgFile, $width, $height, &$error = null) { $attrs = @getimagesize($imgFile); if ($attrs == false) { $error = "Uploaded file does not appear to be an image ($imgFile)"; return false; } if ($attrs[0] * $attrs[1] > 3000000) { $error = "Max pixels allowed is 3,000,000. Your {$attrs[0]} x " . "{$attrs[1]} image has " . $attrs[0] * $attrs[1] . " pixels."; return false; } $ratio = (($attrs[0] / $attrs[1]) < ($width / $height)) ? $width / $attrs[0] : $height / $attrs[1]; $x = max(0, round($attrs[0] / 2 - ($width / 2) / $ratio)); $y = max(0, round($attrs[1] / 2 - ($height / 2) / $ratio)); switch ($attrs[2]) { case IMAGETYPE_JPEG: $src = @imagecreatefromjpeg($imgFile); break;
case IMAGETYPE_PNG: $src = @imagecreatefrompng($imgFile); break;
case IMAGETYPE_GIF: $src = @imagecreatefromgif($imgFile); break;
default: $error = "File is not a JPEG, PNG, or GIF image"; return false; } if ($src == false) { $error = "An error occurred processing the file, it may have been corrupted."; return false; } $resized = imagecreatetruecolor($width, $height); $result = imagecopyresampled($resized, $src, 0, 0, $x, $y, $width, $height, round($width / $ratio, 0) , round($height / $ratio)); if ($result == false) { $error = "Error trying to resize and crop image."; return false; } else { return $resized; } } /** * Merge two thumbnails together * @return resource * @param resource $old * @param resource $new * @param int $width */ function mergeThumbs($old, $new, $width) { $heightOld = imagesy($old); $heightNew = imagesy($new); $heightMerged = $heightOld + $heightNew; $imgMerged = imagecreatetruecolor($width, $heightMerged); imagecopy($imgMerged, $old, 0, 0, 0, 0, $width, $heightOld); imagecopy($imgMerged, $new, 0, $heightOld, 0, 0, $width, $heightNew); return $imgMerged; } [/code]
@narutodude000authorOct 16.2010 — #Thanks, I will try it as a project later on. I'm currently trying to optimize my website for speed. I start the project once I am done.