Hey there, I need help to limit the size of images by resizing it to a certain height and width. I have the code below. What can be done to the code to resize the image when a user upload it to my site? Thanks <? $cntarr=getPics($ses_username); $totcnt=$cntarr[0]+$cntarr[1]; $maxpics=getSetting("MAXPICTURES"); $thumbheight=getSetting("THUMBHEIGHT"); $thumbwidth=getSetting("THUMBWIDTH"); $sql="select * from photos where username='$ses_username' and approved='Y'"; $res=mysql_query($sql); $num=0; $numcolumns=2; while($obj=mysql_fetch_object($res)) { if($obj->url=="") { $catarr[$num][0]="<a target='_blank' href='pics/$obj->filename'><img border=0 class=mytable height=$thumbheight width=$thumbwidth src='pics/$obj->filename'></a>"; } else { $catarr[$num][0]="<a target='_blank' href='$obj->url'><img border=0 class=mytable height=$thumbheight width=$thumbwidth src='$obj->url'></a>"; } $catarr[$num++][1]="<a href='index.php?cmd=21&id=$obj->photosid'>Delete</a>"; } for($i=0;(($num%$numcolumns)!=0);$i++) { $catarr[$num][0]=" "; $catarr[$num][1]=" "; $num++; } ?> <script language="JavaScript"> function checkifvalid(){ if ((window.document.myform.url.value=="")&& (window.document.myform.uppic.value=="")) { alert("please enter the image url or select a picture!"); return false; } } </script> <table width="100%" border="0" align="left" cellpadding="0" cellspacing="0"> <tr> <td width="7%" height="239" rowspan="3" align="center" valign="top"> <div align="center"><br> <br> <br> </div></td> <td width="93%" align="left" valign="top"> <table width="100%" height="100" border="0" cellpadding="0" cellspacing="0"> <tr> <td><br> <table width="80%" border="0" align="left" cellpadding="0" cellspacing="0" class="mytable"> <tr> <td valign="top"> <div align="center"> <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0"> <tr > <td width="1" class="header"> </td> <td height="20" colspan="2" class="header">My Pictures</td> </tr> <tr > <td height="25" class="normal"> </td> <td height="25" colspan="2" class="normal">You have <strong> <? print $cntarr[0];?> </strong> approved pictures and <strong><? print $cntarr[1];?></strong> pictures awaiting approval.</td> </tr> <? if($cntarr[0]>0) { for($i=0;$i<$num;$i=$i+$numcolumns) { ?> <tr > <td height="25" class="normal"> </td> <td width="50%" height="25" align="center" class="normal"><? print $catarr[$i][0]; ?><br> <? print $catarr[$i][1]; ?></td> <td width="50%" height="19" align="center" class="normal"><? print $catarr[$i+1][0]; ?><br> <? print $catarr[$i+1][1]; ?></td> </tr> <? } } ?> </table> </div></td> </tr> </table></td> </tr> <tr> <td> </td> </tr> <tr> <td> <? if($totcnt<$maxpics) { ?> <table width="80%" height="10" border="0" align="left" cellpadding="0" cellspacing="0" class="mytable"> <tr> <td valign="top"> <div align="center"> <table width="100%" height="100%" border="0" align="left" cellpadding="0" cellspacing="0"> <form name="myform" action="index.php?cmd=6" method="post" onsubmit="javascript: return checkifvalid();" enctype="multipart/form-data" > <input type="hidden" name="addpic" value="1"> <tr > <td width="1" class="header"> </td> <td height="20" colspan="2" class="header">Add a Picture</td> </tr> <? if($uperror!="") { ?> <tr > <td height="25" class="normal"> </td> <td height="25" class="normal" colspan="2"><b><? print $uperror; ?></b></td> </tr> <? } ?> <tr > <td height="25" class="normal"> </td> <td height="25" class="normal">Image URL</td> <td height="19" align="left" class="normal"> <input name="url" type="text" class="mytext" id="url" size="40"></td> </tr> <tr > <td height="25" class="normal"> </td> <td height="25" colspan="2" class="normal">for eg.http://www.abc.com/abc.gif</td> </tr> <tr > <td width="1" height="25" class="normal"> </td> <td width="135" height="25" class="normal"><strong>OR</strong> Select a Picture</td> <td width="281" height="19" align="left" class="normal"> <input class="mytext" name="uppic" type="file" id="uppic"></td> </tr> <tr align="center" > <td height="25" colspan="3" class="normal"> <input type="submit" name="Submit" value="Add Picture" class="mybutton"></td> </tr> </form> </table> </div></td> </tr> </table> <? } ?> </td> </tr> <tr> <td> </td> </tr> </table></td> </tr> </table> <br> PHP:
I do something like this to resize images: $scaledImage = imagecreatetruecolor($newWidth, $newHeight); $image = imagecreatefromjpeg($fullPath); imagecopyresampled($scaledImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); imagejpeg($scaledImage, $newFilename, 100); PHP: it will resize image to $newWidth and $newHeight, and save it to $newFilename
if you're going to do many conversions on shared hosting, you will hog the CPU by using GD functions (in the example by krzyk). So in that case, use ImageMagick(.org) instead, with a 'nice' command preceding it; function IMconvert ($sourceFileName, $destFileName, $sizeX, $sizeY, $extraFilter) { $fncn = "IMconvert()"; $destFileName = strtr ($destFileName, "\\", "/"); $destFileName = strtr ($destFileName, "\\\\", "/"); $destDir = substr ($destFileName, 0, strrpos($destFileName, "/")); if (file_exists($destFileName)) return true; if (!file_exists($sourceFileName)) trigger_error ("$fncn : invalid sourceFileName", E_USER_ERROR); if (!createDirectoryStructure ($destDir)) trigger_error ("$fncn : could not create directory for $destFileName", E_USER_ERROR); htmlOut ("<div id='convert' class='progressComment'>Converting to thumbnail, $sizeX x $sizeY pixels.</div>", 0, $hrLog); $cmd = "nice -n 19 convert -size ".$sizeX."x".$sizeY." \"$sourceFileName\" -resize ".$sizeX."x".$sizeY." $extraFilter \"$destFileName\""; unset ($output); $result = -1; $fails = 0; while (($result!=0) && ($fails <= 5)) { exec ($cmd, $output, $result); if ($result!=0) { //$eventHTML = "<div>$cmd</div><div>returned code $result and<br/>$output</div>"; //mbLogTechnical ("failed: ImageMagick convert (will retry)", MB_LOG_RESOURCE_FAILURE, null, $eventHTML); //htmlOut ("<div id='progressHickup' class='progressComment'>ImageMagick convert failed; auto-retry in ".mbConfig("sp_import_step_delay_seconds")." seconds.</div>", 0, $hrLog); $fails++; sleep (mbConfig("sp_import_step_delay")); } if ($fails >= 5) { //htmlOut ("<div id='progressHickup' class='progressComment'>ImageMagick convert failed for $destFileName", 0, $hrLog); return false; } } return true; } Code (markup): In case you're wondering why the code can retry up to 5 times; shared hosting sometimes kills a valid process without warning. The code then retries.
Thanks guys, rene7705 how do I add your code to the code I supplied? Can you please show me how to add the code Thanks
Probably like... $sourceFileName = $_FILES['myupload']['tmp_name']; // or however your upload is named after validating it $destFileName = 'SaveMeHere.jpg'; // add your directory too! $sizeX = '600'; $sizeY = '400'; $extraFilter = ''; // not to sure about here $result = function IMconvert($sourceFileName, $destFileName, $sizeX, $sizeY, $extraFilter); Code (markup):