OSdata.com |
Automatically resizing a picture: You are supposed to have every picture on your website match the size specified in an image tag. No resizing with CSS or HTML. This script can be modified to automatically resize any image on your website.
Building a game open source code This is the actual source code from a new web game. See the game at thissideofsanity.com and read how this was built starting at example code.
This is example code from the SlamZee project and This Side of Sanity, released under Apache License 2.0.
Copyright 2013 Milo (for software), Distribution and website handled by Strazbick.com
Licensed under the Apache License, Version 2.0 (the License); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
This narrative uses anchor links so that you can follow the building of this software in chronological order. Go to software explanation to start reading. Go to thissideofsanity.com to see the working software.
This is example code from the SlamZee project and This Side of Sanity, released under Apache License 2.0.
Automatically resizing a picture: You are supposed to have every picture on your website match the size specified in an image tag. No resizing with CSS or HTML. This script can be modified to automatically resize any image on your website.
Robert Dinu correctly points out that this script is computationally intensive. I do plan on adding a follow-up BASH script and a follow-up PHP script, each of which collect a batch of file names to resize (with new sizes) and then use cURL to run this script and collect the resulting pictures to store them in an appropriate location. Until I get those scripts written, that is left as an exercise for the reader.
It is considered good etiquette to always serve up images in the size they will appear in the web browser (never using CSS or HTML resizing). This is particularly important for mobile devices (especially older smart phones) and those in areas with poor reception/wi-fi/internet service.
This is also needed to meet Section 508 and American with Disabilities (ADA) compliance validation -- I never understood why, but they include this.
So, what happens if you need thumbnails of large pictures in a bunch of different sizes for different web pages on your website?
Do you really whip out Photoshop and make bunches of different sets of thumbnails by hand?
No. You use a computer your server. Use hand-crafting for things that are only done once and use automation for things that are done repeatedly. Thats what computers are good for.
This is generalized code, but you will probably still need to modify the source code for your purposes, but all of the basics are provided for your use.
Enjoy and share.
<?php /* AUTO RESIZE OF PICTURE */ /* make sure there are NO characters (spaces, newlines) before or after the PHP */ /* because you are going to send a header */ /* called in the form of */ /* <img src="http://www.example.com/autoresizepicture.php?source=sourcepicture.png&newwidth=90&newheight=90" width="90" height="90"> */ /* probably should add some error checking -- coding left for implementor */ $source_url = $_GET['source']; /* the location of the source picture */ $newwidth = $_GET['newwidth']; /* the new width for the final picture */ $newheight = $_GET['newheight']; /* the new height for the final picture */ /* note that the image might get distorted if the new width and height */ /* don't keep at the same proportions as the orginal image */ $sourcesize = getimagesize($source_url); /* gets an array of height and width of unsized original picture */ /* automatically set the header */ /* key two holds the image type constant */ switch ($sourcesize[2]) { case IMAGETYPE_PNG: /* PNG = 3 */ header("content-type: image/png"); break; case IMAGETYPE_JPEG: /* JPG = 2 */ header("content-type: image/jpeg"); break; case IMAGETYPE_GIF: /* GIF = 1 */ header("content-type: image/gif"); break; default: exit; /* can't handle any other type */ break; } /* END SWITCH */ /* you, of course, added error checking to insure the picture exists */ /* because you never trust user input */ /* also, make sure they aren't fetching a picture that isn't for the public */ switch ($sourcesize[2]) { case IMAGETYPE_PNG: /* PNG = 3 */ $source_image = imagecreatefrompng($source_url); break; case IMAGETYPE_JPEG: /* JPG = 2 */ $source_image = imagecreatefromjpeg($source_url); break; case IMAGETYPE_GIF: /* GIF = 1 */ $source_image = imagecreatefromgif($source_url); break; default: exit; /* can't handle any other type */ break; } /* END SWITCH */ $destination_image = imagecreatetruecolor($newwidth,$newheight); /* you, of course, added error checking to insure reasonable sizes */ /* the parameters for the imagecopyresized function are (in order): */ /* resource destination image */ /* resource source image */ /* int destination start x */ /* int destination start y */ /* those are upper left coordinates */ /* int source start x */ /* int source start y */ /* int destination width */ /* int destination height */ /* those are lower right coordinates */ /* int source width */ /* int source height */ imagecopyresampled($destination_image, $source_image, 0, 0, 0, 0, $newwidth, $newheight, $sourcesize[0], $sourcesize[1]); /* instruct the server to output the new image as a PNG or JPEG or GIF */ switch ($sourcesize[2]) { case 'p': imagepng($destination_image); break; case 'j': imagejpeg($destination_image); break; case 'g': imagegif($destination_image); break; default: exit; /* can't handle any other type */ break; } /* END SWITCH */ /* always manually destroy the resources */ /* PHP does NOT clean these up automatically */ /* failure to destroy will cause a memory leak */ /* your server will eventually run out of memory, slowing and stopping */ imagedestroy($source_image); imagedestroy($destination_image); /* it's really that easy */ /* modify script for your real world use */ /* please keep the Apache 2.0 License message */ /* and share with others */ ?>
OSdata.com is used in more than 300 colleges and universities around the worldFind out how to get similar high web traffic and search engine placement. |
OSdata.com is used in more than 300 colleges and universities around the world |
Tweets by @osdata |
A web site on dozens of operating systems simply cant be maintained by one person. This is a cooperative effort. If you spot an error in fact, grammar, syntax, or spelling, or a broken link, or have additional information, commentary, or constructive criticism, please e-mail Milo. If you have any extra copies of docs, manuals, or other materials that can assist in accuracy and completeness, please send them to Milo, PO Box 1361, Tustin, CA, USA, 92781.
Click here for our privacy policy.
previous page | next page |
This web site handcrafted on Macintosh computers using Tom Benders Tex-Edit Plus and served using FreeBSD .
Names and logos of various OSs are trademarks of their respective owners.
Copyright © 2013 Milo
Last Updated: December 12, 2013
Created: December 12, 2013
previous page | next page |