Resize images in PHP without using third-party libraries? -
in 1 of applications, i'm using code snippet below copy uploaded images directory. works fine copying large images (> 2mb) takes more time ideal , don't need images big, so, i'm looking way resize images. how achieve using php?
<?php $uploaddirectory = 'images/0001/'; $randomnumber = rand(0, 99999); $filename = basename($_files['userfile']['name']); $filepath = $uploaddirectory.md5($randomnumber.$filename); // check if file sent through http post. if (is_uploaded_file($_files['userfile']['tmp_name']) == true) { // validate file size, accept files under 5 mb (~5e+6 bytes). if ($_files['userfile']['size'] <= 5000000) { // move file path specified. if (move_uploaded_file($_files['userfile']['tmp_name'], $filepath) == true) { // ... } } } ?>
finally, i've discovered way fit needs. following snippet resize image specified width, automatically calculating height in order keep proportion.
$image = $_files["image"]["tmp_name"]; $resizeddestination = $uploaddirectory.md5($randomnumber.$filename)."_resized.jpg"; copy($_files, $resizeddestination); $imagesize = getimagesize($image); $imagewidth = $imagesize[0]; $imageheight = $imagesize[1]; $desired_width = 100; $proportionalheight = round(($desired_width * $imageheight) / $imagewidth); $originalimage = imagecreatefromjpeg($image); $resizedimage = imagecreatetruecolor($desired_width, $proportionalheight); imagecopyresampled($images_fin, $originalimage, 0, 0, 0, 0, $desired_width+1, $proportionalheight+1, $imagewidth, $imageheight); imagejpeg($resizedimage, $resizeddestination); imagedestroy($originalimage); imagedestroy($resizedimage);
to else seeking complete example, create 2 files:
<!-- send.html --> <html> <head> <title>simple file upload</title> </head> <body> <center> <div style="margin-top:50px; padding:20px; border:1px solid #cecece;"> select image. <br/> <br/> <form action="receive.php" enctype="multipart/form-data" method="post"> <input type="file" name="image" size="40"> <input type="submit" value="send"> </form> </div> </center> </body>
<?php // receive.php $randomnumber = rand(0, 99999); $uploaddirectory = "images/"; $filename = basename($_files['file_contents']['name']); $destination = $uploaddirectory.md5($randomnumber.$filename).".jpg"; echo "file path:".$filepath."<br/>"; if (is_uploaded_file($_files["image"]["tmp_name"]) == true) { echo "file received through http post.<br/>"; // validate file size, accept files under 5 mb (~5e+6 bytes). if ($_files['image']['size'] <= 5000000) { echo "file size: ".$_files["image"]["size"]." bytes.<br/>"; // resize , save image. $image = $_files["image"]["tmp_name"]; $resizeddestination = $uploaddirectory.md5($randomnumber.$filename)."_resized.jpg"; copy($_files, $resizeddestination); $imagesize = getimagesize($image); $imagewidth = $imagesize[0]; $imageheight = $imagesize[1]; $desired_width = 100; $proportionalheight = round(($desired_width * $imageheight) / $imagewidth); $originalimage = imagecreatefromjpeg($image); $resizedimage = imagecreatetruecolor($desired_width, $proportionalheight); imagecopyresampled($images_fin, $originalimage, 0, 0, 0, 0, $desired_width+1, $proportionalheight+1, $imagewidth, $imageheight); imagejpeg($resizedimage, $resizeddestination); imagedestroy($originalimage); imagedestroy($resizedimage); // save original image. if (move_uploaded_file($_files['image']['tmp_name'], $destination) == true) { echo "copied original file specified destination.<br/>"; } } } ?>
Comments
Post a Comment