note 111988 added to function.imagecopyresized

From: Date: Fri, 19 Apr 2013 06:43:08 +0000
Subject: note 111988 added to function.imagecopyresized
Groups: php.notes 
Request: Send a blank email to php-notes+get-194559@lists.php.net to get a copy of this message
There are many user notes here that deal with creating a thumbnail, but I wrote a function that created a thumbnail of an image as it gets uploaded. It is simply a combination of a file uploader with a thumbnail maker, but it is very simple to use now. Following is an index.php file that contains the upload form, the function, and an example of how to use the function. The last two values in the 'thumb' function are for the width and height limits for the resulting thumbnail. No matter what size the original image, the thumbnail will always fit within the dimensions specified. <?php if ($_POST[formname] =="upload") { if ($_FILES) { foreach ($_FILES as $key => $value) { $m = thumb($value, realpath(dirname(__FILE__)) . "/" . "image_1", realpath(dirname(__FILE__)) . "/" . "thumb_1", 150, 150); // display any messages, whether success or failure if ($m) { foreach($m as $text) { print "$text<br>"; } } } } } function thumb($file, $bigname, $thumbname, $maxwidth, $maxheight) { if (is_uploaded_file($file[tmp_name])) { // determine the image type if ($file[type] == "image/jpeg") { $ext = ".jpg"; } if ($file[type] == "image/png") { $ext = ".png"; } if ($file[type] == "image/gif") { $ext = ".gif"; } $newfile = $bigname . $ext; if (move_uploaded_file($file[tmp_name], $newfile)) { $message[] = "Upload complete"; // get old image size and convert to a maximum width/height combo $size = getimagesize($newfile); // only do the conversion if the width is greater than zero, otherwise there could be a division by zero error if ($size[1] != 0) { // Calculate the thumbnail's width an height, based on the max sized allowed and the original's dimensions if ($size[0]/$size[1] > $maxwidth/$maxheight) { $newwidth = $maxwidth; $newheight = floor(($size[1] * $maxwidth) / $size[0]); } else { $newheight = $maxheight; $newwidth = ceil(($size[0] * $maxheight) / $size[1]); } // Create the thumbnail $i = imagecreate($newwidth, $newheight); if ($ext == ".jpg") { $j = imagecreatefromjpeg($newfile); } if ($ext == ".png") { $j = imagecreatefrompng($newfile); } if ($ext == ".gif") { $j = imagecreatefromgif($newfile); } if ($j) { // Copy the original image and paste into the new image, resized if (imagecopyresized($i, $j, 0, 0, 0, 0, $newwidth, $newheight, $size[0], $size[1])) { if (imagejpeg($i, $thumbname . $ext, 100)) { $message[] = "Image thumb created successfully"; } else { $message[] = "Image thumb would not export"; } } else { $message[] = "Image resize failure"; } } else { $message[] = "Image type not recognized"; } } else { // size[1] != 0 $message[] = "Image size invalid"; } } // move_uploaded_file } // is_uploaded_file return $message; } print " <html> <head> <title>Image upload and create Thumbnail</title> </head> <body> <form action='index.php' method='POST' enctype='multipart/form-data'> <input type='hidden' name='formname' value='upload'> <input type='file' name='fileupload'> <input type='submit' value='Upload'> </form> </body> </html> "; ?> NOTE: This does not deal with adjusting palettes or transparent colors, but that can be added if you need it. ---- Server IP: 72.55.140.6 Probable Submitter: 68.150.37.202 ---- Manual Page -- http://php.net/manual/en/function.imagecopyresized.php Edit -- https://master.php.net/note/edit/111988 Del: integrated -- https://master.php.net/note/delete/111988/integrated Del: useless -- https://master.php.net/note/delete/111988/useless Del: bad code -- https://master.php.net/note/delete/111988/bad+code Del: spam -- https://master.php.net/note/delete/111988/spam Del: non-english -- https://master.php.net/note/delete/111988/non-english Del: in docs -- https://master.php.net/note/delete/111988/in+docs Del: other reasons-- https://master.php.net/note/delete/111988 Reject -- https://master.php.net/note/reject/111988 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#194559) next »