Learning & Integrating web technology and code help directory

How to use PHP & Resize image Thumbnails and insert to MySQL

No comments
How to use PHP & Resize image Thumbnails and insert to MySQL The guideline/example scripts how to using PHP and GD , Resize image Thumbnails and insert to MySQL
Example
php_resize_mysql1.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <form name="form1" method="post" action="php_resize_mysql2.php" enctype="multipart/form-data">  
  7. <input type="file" name="fileUpload"><br>  
  8. <input name="btnSubmit" type="submit" value="Submit">  
  9. </form>  
  10. </body>  
  11. </html>  
php_resize_mysql2.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. if(trim($_FILES["fileUpload"]["tmp_name"]) != "")  
  8. {  
  9. $images = $_FILES["fileUpload"]["tmp_name"];  
  10. $new_images = "thumbnails_".$_FILES["fileUpload"]["name"];  
  11. copy($_FILES["fileUpload"]["tmp_name"],"MyResize/".$_FILES["fileUpload"]["name"]);  
  12. $width=100; //*** Fix Width & Heigh (Autu caculate) ***//  
  13. $size=GetimageSize($images);  
  14. $height=round($width*$size[1]/$size[0]);  
  15. $images_orig = ImageCreateFromJPEG($images);  
  16. $photoX = ImagesX($images_orig);  
  17. $photoY = ImagesY($images_orig);  
  18. $images_fin = ImageCreateTrueColor($width$height);  
  19. ImageCopyResampled($images_fin$images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX$photoY);  
  20. ImageJPEG($images_fin,"MyResize/".$new_images);  
  21. ImageDestroy($images_orig);  
  22. ImageDestroy($images_fin);  
  23.   
  24. echo "Resize Successful.<br>";  
  25.   
  26. //*** Insert Record ***//  
  27. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error());  
  28. $objDB = mysql_select_db("mydatabase");  
  29. $strSQL = "INSERT INTO files ";  
  30. $strSQL .="(Thumbnails,FilesName) VALUES ('".$new_images."','".$_FILES["fileUpload"]["name"]."')";  
  31. $objQuery = mysql_query($strSQL);  
  32. }  
  33.   
  34. ?>  
  35. <a href="php_resize_mysql3.php">View files</a>  
  36. </body>  
  37. </html>  
php_resize_mysql3.php

  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error());  
  8. $objDB = mysql_select_db("mydatabase");  
  9. $strSQL = "SELECT * FROM files";  
  10. $objQuery = mysql_query($strSQLor die ("Error Query [".$strSQL."]");  
  11. ?>  
  12. <table width="200" border="1">  
  13. <tr>  
  14. <th width="50"> <div align="center">Files ID </div></th>  
  15. <th width="150"> <div align="center">Thumbnails </div></th>  
  16. </tr>  
  17. <?  
  18. while($objResult = mysql_fetch_array($objQuery))  
  19. {  
  20. ?>  
  21. <tr>  
  22. <td><div align="center"><?=$objResult["FilesID"];?></div></td>  
  23. <td><center><a href="MyResize/<?=$objResult["FilesName"];?>">  
  24. <img src="MyResize/<?=$objResult["Thumbnails"];?>" border="0"></a></center></td>  
  25. </tr>  
  26. <?  
  27. }  
  28. ?>  
  29. </table>  
  30. <?  
  31. mysql_close($objConnect);  
  32. ?>  
  33. <br>  
  34. <a href="php_resize_mysql1.php">Upload Image</a>  
  35. </body>  
  36. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP & Resize image Thumbnails and insert to MySQL
PHP & Resize image Thumbnails and insert to MySQL

No comments :

Post a Comment