php - Save cropped image in different width and height -
i using jcrop crop images.
this form upload image , crop.
<form id="upload_form" enctype="multipart/form-data" method="post" action="upload.php" onsubmit="return checkform()"> <!-- hidden crop params --> <input type="hidden" id="x1" name="x1" /> <input type="hidden" id="y1" name="y1" /> <input type="hidden" id="x2" name="x2" /> <input type="hidden" id="y2" name="y2" /> <div><input type="file" name="image_file" id="image_file" onchange="fileselecthandler()" /></div> <div class="error"></div> <div class="step2"> <h2>step2: please select crop region</h2> <img id="preview" /> <div class="info"> <label>file size</label> <input type="text" id="filesize" name="filesize" /> <label>type</label> <input type="text" id="filetype" name="filetype" /> <label>image dimension</label> <input type="text" id="filedim" name="filedim" /> <label>w</label> <input type="text" id="w" name="w" /> <label>h</label> <input type="text" id="h" name="h" /> </div> <input type="submit" value="upload" /> </div> </form>
upload.php file upload cropped image avatar directory.
<?php function uploadimagefile() { // note: gd library required function if ($_server['request_method'] == 'post') { $iwidth = $iheight = 200; // desired image result dimensions $ijpgquality = 90; if ($_files) { // if no errors , size less 250kb if (! $_files['image_file']['error'] && $_files['image_file']['size'] < 250 * 1024) { if (is_uploaded_file($_files['image_file']['tmp_name'])) { // new unique filename $stempfilename = 'avatar/' . md5(time().rand()); // move uploaded file cache folder move_uploaded_file($_files['image_file']['tmp_name'], $stempfilename); // change file permission 644 @chmod($stempfilename, 0644); if (file_exists($stempfilename) && filesize($stempfilename) > 0) { $asize = getimagesize($stempfilename); // try obtain image info if (!$asize) { @unlink($stempfilename); return; } // check image type switch($asize[2]) { case imagetype_jpeg: $sext = '.jpg'; // create new image file $vimg = @imagecreatefromjpeg($stempfilename); break; /*case imagetype_gif: $sext = '.gif'; // create new image file $vimg = @imagecreatefromgif($stempfilename); break;*/ case imagetype_png: $sext = '.png'; // create new image file $vimg = @imagecreatefrompng($stempfilename); break; default: @unlink($stempfilename); return; } // create new true color image $vdstimg = @imagecreatetruecolor( $iwidth, $iheight ); // copy , resize part of image resampling imagecopyresampled($vdstimg, $vimg, 0, 0, (int)$_post['x1'], (int)$_post['y1'], $iwidth, $iheight, (int)$_post['w'], (int)$_post['h']); // define result image filename $sresultfilename = $stempfilename . $sext; // output image file imagejpeg($vdstimg, $sresultfilename, $ijpgquality); @unlink($stempfilename); return $sresultfilename; } } } } } } $simage = uploadimagefile(); echo '<img src="'.$simage.'" />'; ?>
my question:
right upload cropped image in avatar directory width , height of 200px.
i want upload cropped image in 2 other directories
avatar1 width , height of 500px
avatar2 width , height of 700px
any appreciated.
add arguments function, e.g. uploadimagefile($dirname, $iwidth, $iheight)
, invoke multiple times different sizes
<?php function uploadimagefile() { // note: gd library required function if ($_server['request_method'] == 'post') { $ijpgquality = 90; if ($_files) { // if no errors , size less 250kb if (! $_files['image_file']['error'] && $_files['image_file']['size'] < 250 * 1024) { if (is_uploaded_file($_files['image_file']['tmp_name'])) { if (!is_dir('avatar')) { mkdir('avatar'); } // new unique filename $stempfilename = 'avatar/' . md5(time().rand()); // move uploaded file cache folder move_uploaded_file($_files['image_file']['tmp_name'], $stempfilename); // change file permission 644 @chmod($stempfilename, 0644); $sresultfilename = copyimagefile('avatar', $stempfilename, 200, 200, $ijpgquality); if ($sresultfilename) { copyimagefile('avatar1', $stempfilename, 500, 500); copyimagefile('avatar2', $stempfilename, 700, 700); @unlink($stempfilename); return $sresultfilename; } } } } } return false; } function copyimagefile($dirname, $originimagename, $iwidth, $iheight, $ijpgquality = 90) { if (file_exists($originimagename) && filesize($originimagename) > 0) { $asize = getimagesize($originimagename); // try obtain image info if (!$asize) { @unlink($originimagename); return; } // check image type switch($asize[2]) { case imagetype_jpeg: $sext = '.jpg'; $vimg = @imagecreatefromjpeg($originimagename); break; /*case imagetype_gif: $sext = '.gif'; // create new image file $vimg = @imagecreatefromgif($stempfilename); break;*/ case imagetype_png: $sext = '.png'; $vimg = @imagecreatefrompng($originimagename); break; default: @unlink($originimagename); return; } // create new true color image $vdstimg = @imagecreatetruecolor( $iwidth, $iheight ); // copy , resize part of image resampling imagecopyresampled($vdstimg, $vimg, 0, 0, (int)$_post['x1'], (int)$_post['y1'], $iwidth, $iheight, (int)$_post['w'], (int)$_post['h']); // define result image filename if (!is_dir($dirname)) { mkdir($dirname); } $newimagename = $dirname . directory_separator . md5(time().rand()) . $sext; // output image file imagejpeg($vdstimg, $newimagename, $ijpgquality); //@unlink($stempfilename); return $newimagename; } return false; } $simage = uploadimagefile(); echo '<img src="'.$simage.'" />'; ?>
Comments
Post a Comment