Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Скрипт резайза и загрузки изображения на сервер
- */
- ini_set("display_errors", "1");
- ini_set("display_startup_errors", "1");
- ini_set('error_reporting', E_ALL | E_STRICT);
- error_reporting(E_ALL | E_STRICT);
- ini_set('display_errors', 'On');
- $SCRIPT_NAME = 'ava.php';
- if (isset($_SERVER['SCRIPT_NAME'])) {
- $SCRIPT_NAME = $_SERVER['SCRIPT_NAME'];
- }
- $folderVars = array(
- 'uploadFolder' => "./upl/",
- 's_ImgFolder' => "./upl/small/",
- 'small_ImgPrefix' => ''
- );
- $galleryVars = array(
- 'new_imgWidth' => 200,
- 's_ImgWidth' => 100,
- 's_ImgHeight' => 100,
- 'ImgExts' => array('gif', 'jpg', 'png')
- );
- $answer = '';
- /**
- * @param $w1
- * @param $h1
- * @param $w2
- * @param $h2
- * @param bool $crop
- *
- * @return array
- */
- function Tools_getimgwhr($w1, $h1, $w2, $h2, $crop = false) {
- $res = array('w2'=> 0, 'h2'=> 0, 'R1'=> 0, 'R2'=> 0);
- $R1 = $h1 / $w1;
- if ($crop) {
- $R2 = $h2 / $w2;
- } else {
- $R2 = $R1;
- $h2 = $w2 * $R1;
- }
- $res['w2'] = $w2;
- $res['h2'] = $h2;
- $res['R1'] = $R1;
- $res['R2'] = $R2;
- return $res;
- }
- function io_writeImage($type, $filename, $img) {
- $result = false;
- switch ($type) {
- case 'jpg':
- $result = imagejpeg($img, $filename);
- break;
- case 'gif':
- $result = imagegif($img, $filename);
- break;
- case 'png':
- $result = imagepng($img, $filename);
- break;
- }
- return $result;
- }
- function io_readImage($i_type, $filename) {
- $res = false;
- //echo $filename, ' trying read ....';
- switch ($i_type) {
- case 'jpg':
- //echo 'jpg ';
- $res = imagecreatefromjpeg($filename);
- //var_dump($res);
- break;
- case 'gif':
- //echo 'gif ';
- $res = imagecreatefromgif($filename);
- break;
- case 'png':
- //echo 'png ';
- $res = imagecreatefrompng($filename);
- break;
- }
- return $res;
- }
- function tools_imageResize
- (
- $type, $img1,
- $w1, $h1,
- $w2, $h2,
- $crop = false
- ) {
- // //echo '<br/>';
- // //echo 'begin resizing ',$img1,' from ',$w1,' x ',$h1,' to ',$w2,' x ',$h2,' crop = ',$crop,'<br/>';
- $srcH = $srcW = $srcX = $srcY = 0;
- //echo 'src image ', $img1, ' opening...';
- if (is_file($img1)) {
- //echo 'is file<br/>';
- } else {
- //echo 'is not file<br/>';
- }
- $srcImg = io_readImage($type, $img1);
- //var_dump($srcImg);
- // //echo '<br/>';
- $imgWHR = tools_getImgWHR($w1, $h1, $w2, $h2, $crop);
- //echo 'begint calc resizing from ', $w1, ' x ' . $h1, ' to ', $w2, ' x ', $h2, '<hr/>';
- $R1 = $imgWHR['R1'];
- $R2 = $imgWHR['R2'];
- $h2 = $imgWHR['h2'];
- $w2 = $imgWHR['w2'];
- if ($R1 < $R2) {
- $srcY = 0;
- $srcX = floor(($w1 - $h1 / $R2) / 2);
- $srcW = $h1 / $R2;
- $srcH = $h1;
- } elseif ($R1 > $R2) {
- $srcX = 0;
- $srcY = floor(($h1 - $w1 * $R2) / 2);
- $srcW = $w1;
- $srcH = $w1 * $R2;
- } elseif ($R1 == $R2) {
- $srcX = 0;
- $srcY = 0;
- $srcH = $h1;
- $srcW = $w1;
- }
- //echo 'calculated resizing ', $img1, ' from ', $srcW, ' x ', $srcH, ' to ', $w2, ' x ', $h2, ' crop = ', $crop, '<br/>';
- //echo'srcX:Y = ', $srcX, ' : ', $srcY, '<br/>';
- //echo 'src w x h = ', $srcW, ' x ', $srcH, '<br/>';
- //echo 'w2 x h2 = ', $w2, ' x ', $h2;
- $dstImg = imagecreatetruecolor($w2, $h2);
- $r4 = imagecopyresampled($dstImg, $srcImg,
- 0, 0,
- $srcX, $srcY,
- $w2, $h2,
- $srcW, $srcH);
- //echo '<br/>im copy resampled returns ', $r4, '<br/>';
- imagedestroy($srcImg);
- $result['img'] = $dstImg;
- $result['width'] = $w2;
- $result['height'] = $h2;
- return $result;
- }
- function io_chkFolders($folderVars) {
- $r1 = $r2 = true;
- $uploadFolder = $folderVars['uploadFolder'];
- $s_ImgFolder = $folderVars['s_ImgFolder'];
- if (!is_dir($uploadFolder)) {
- $r1 = mkdir($uploadFolder, 0777, true);
- }
- if (!is_dir($s_ImgFolder)) {
- $r2 = mkdir($s_ImgFolder, 0777, true);
- }
- return $r1 && $r2;
- }
- function tools_getFileName($fileId, $ext, $folderVars, $_s = false) {
- $uploadFolder = $folderVars['uploadFolder'];
- $s_ImgFolder = $folderVars['s_ImgFolder'];
- $small_ImgPrefix = $folderVars['small_ImgPrefix'];
- if (!$_s) {
- $result = $uploadFolder . $fileId . '.' . $ext;
- } else {
- $result = $s_ImgFolder . $small_ImgPrefix . $fileId . '.' . $ext;
- }
- return $result;
- }
- /**
- * @param string $fileId filename ot id for database
- * @param string $sTmpName name of uploaded but not moved
- * @param string $sExt file exstension
- * @param int $ImgWidth image width
- * @param int $ImgHeight img height
- * @param mixed $folderVars see up
- * @param mixed $galleryVars see up
- *
- * @return mixed
- *
- */
- function Io_addimage
- (
- $fileId, $sTmpName,
- $sExt, $ImgWidth, $ImgHeight,
- $folderVars, $galleryVars
- ) {
- $new_imgWidth = $galleryVars['new_imgWidth'];
- $s_ImgWidth = $galleryVars['s_ImgWidth'];
- $s_ImgHeight = $galleryVars['s_ImgHeight'];
- $sNewName = tools_getFileName($fileId, $sExt, $folderVars);
- $s_ImgFilename = tools_getFileName($fileId, $sExt, $folderVars, true);
- //echo '<hr/>New Name is ', $sNewName, ' new small img name is ', $s_ImgFilename, '<hr/>';
- $upl_moved = false;
- $r1 = io_chkFolders($folderVars);
- //echo 'folders check...', $r1, '<br/>';
- if ($ImgWidth > $new_imgWidth) {
- //echo '$ImgWidth > ', $new_imgWidth, '<br/>';
- $resize = tools_imageResize(
- $sExt, $sTmpName, $ImgWidth, $ImgHeight, $new_imgWidth, $ImgHeight
- );
- //echo 'resize result: <br/>';
- //var_dump($resize);
- //echo '<br/>';
- $new_Img = $resize['img'];
- $big_width = $resize['width'];
- $big_height = $resize['height'];
- $ImgHeight = $big_height;
- $ImgWidth = $big_width;
- //echo 'Resing complete, w x h = ', $width, ' x ', $height, '<br/>';
- $r2 = io_writeImage($sExt, $sNewName, $new_Img);
- //echo 'trying to write ...', $r2, '<br/>';
- imagedestroy($new_Img);
- //echo '$resize width ', $width, ' height ', $height, ' $result ', $r2;
- } else {
- //echo 'img width < ', $new_imgWidth, '<br/>';
- //echo $sTmpName, '<br/>';
- $upl_moved
- = $r2 = move_uploaded_file($sTmpName, $sNewName);
- //echo $sNewName, ' writing big image...', $r2, '<br/>';
- }
- chmod($sNewName, 0777);
- //echo '<hr/>small IMAGE<hr/>';
- if (($ImgWidth > $s_ImgWidth) && ($ImgHeight > $s_ImgHeight)) {
- //echo "($ImgWidth > $s_ImgWidth) && ($ImgHeight) > $s_ImgHeight";
- //echo '<hr/>';
- $resize = tools_imageResize(
- $sExt, $sNewName, $ImgWidth, $ImgHeight, $s_ImgWidth, $s_ImgHeight, true
- );
- $s_Img = $resize['img'];
- $small_width = $resize['width'];
- $small_height = $resize['height'];
- $s_ImgWidth = $small_width;
- $s_ImgHeight = $small_height;
- //var_dump($resize);
- $r3 = io_writeImage($sExt, $s_ImgFilename, $s_Img);
- //echo 'io_writeImage writeing small image...', $r3, '<br/>';
- imagedestroy($s_Img);
- } else {
- //echo "($ImgWidth < $s_ImgWidth) ||($ImgHeight) < $s_ImgHeight";
- //echo '<hr/>';
- $s_ImgWidth = $ImgWidth;
- $s_ImgHeight = $ImgHeight;
- if ($upl_moved) {
- //echo 'already uploaded, copying...';
- $r3 = copy($sNewName, $s_ImgFilename);
- //echo $r3, '<br/>';
- } else {
- //echo $sTmpName, '<br/>';
- $r3 = move_uploaded_file($sTmpName, $s_ImgFilename);
- //echo $s_ImgFilename, ' writing small image...', $r3, '<br/>';
- }
- }
- chmod($s_ImgFilename, 0777);
- /* $imgRes['color_list'] = getColorsList($sExt,$sNewName,$ImgWidth,$ImgHeight);*/
- $imgRes['width'] = $ImgWidth;
- $imgRes['height'] = $ImgHeight;
- $imgRes['small_width'] = $s_ImgWidth;
- $imgRes['small_height'] = $s_ImgHeight;
- $imgRes['success'] = $r1 && $r2 && $r3;
- //echo 'success = ', $imgRes['success'], '<hr/>';
- return $imgRes;
- }
- /**
- * @param string $filename
- *
- * @return array|bool
- *
- */
- function Tools_imageinfo
- (
- $filename = 'null'
- ) {
- if (!is_file($filename)) {
- //var_dump($filename);
- //echo $filename, ' is not a file. ';
- return false;
- }
- if (!$data = getimagesize($filename) or !$filesize = filesize($filename)) {
- //echo 'file size error ';
- return false;
- }
- $extensions = array(
- 1 => 'gif',
- 2 => 'jpg',
- 3 => 'png',
- 4 => 'swf',
- 5 => 'psd',
- 6 => 'bmp',
- 7 => 'tiff',
- 8 => 'tiff',
- 9 => 'jpc',
- 10 => 'jp2',
- 11 => 'jpx',
- 12 => 'jb2',
- 13 => 'swc',
- 14 => 'iff',
- 15 => 'wbmp',
- 16 => 'xbmp'
- );
- $result = array(
- 'width' => $data[0],
- 'height' => $data[1],
- 'ext' => $extensions[$data[2]],
- 'size' => $filesize,
- 'mime' => $data['mime']);
- return $result;
- }
- /**
- * @param int $bytes bytes
- * @param int $precision precision
- *
- * @return string
- *
- */
- function Tools_bytestosize1024($bytes, $precision = 2) {
- $u = array('B', 'KB', 'MB');
- $i = (int)floor(log($bytes, 1024));
- return round($bytes / pow(1024, $i), $precision) . ' ' . $u[$i];
- }
- if (isset($_FILES['element_1'])) {
- $sTmpName = explode(".", $_FILES['element_1']['name']);
- $sFileName = $sTmpName[0];
- $sTmpName = $_FILES['element_1']['tmp_name'];
- $ImgExts = $galleryVars['ImgExts'];
- $imageInfo = tools_imageInfo($sTmpName);
- if (!$imageInfo
- or !in_array($imageInfo['ext'], $ImgExts)
- ) {
- die('Неподдерживаемый формат изображения. ');
- }
- $sExt = $imageInfo['ext'];
- $ImgWidth = $imageInfo['width'];
- $ImgHeight = $imageInfo['height'];
- $sFileSize = $imageInfo['size'];
- $img_res = io_addImage(
- $sFileName, $sTmpName,
- $sExt, $ImgWidth, $ImgHeight,
- $folderVars, $galleryVars
- );
- $new_width = $img_res['width'];
- $new_height = $img_res['height'];
- $small_width = $img_res['small_width'];
- $small_height = $img_res['small_height'];
- //echo '<hr/>hi';
- //echo '<pre>io_addImage<br/>';
- //var_dump($Img_res2);
- //echo'</pre>';
- //echo '<hr/>';
- $sFileSize = tools_bytesToSize1024($sFileSize, 1);
- $uploaded_filename = tools_getFileName($sFileName, $sExt, $folderVars, false);
- $uploaded_small_filename = tools_getFileName($sFileName, $sExt, $folderVars, true);
- ob_start(false);
- echo <<<EOF
- <div class="s">
- <p>Ваш файл: {$sFileName} передан удачно.</p>
- <p>Оригинальное разрешение: {$ImgWidth} x {$ImgHeight} </p>
- <p>Размер: {$sFileSize}</p>
- <img src="{$uploaded_filename}"></img>
- <p>Разрешение изображения на сервере: {$new_width} x {$new_height} </p>
- <img src="{$uploaded_small_filename}"></img>
- <p>Разрешение уменьшенной копии: {$small_width} x {$small_height} </p>
- EOF;
- echo "</div>";
- $answer = ob_get_flush();
- ob_clean();
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Загрузить изображение</title>
- <link rel="stylesheet" type="text/css" href="./colors/color1/view.css">
- </head>
- <body id="main_body">
- <img id="top" src="images/top.png" alt="">
- <div id="form_container">
- <h1><a>Загрузить изображение</a></h1>
- <?=$answer;?>
- <form id="form_490795" class="appnitro" enctype="multipart/form-data" method="post"
- action="<?php //echo $SCRIPT_NAME; ?>">
- <div class="form_description">
- <h2>Загрузить изображение</h2>
- <p>Выберите файл для загрузки. Разрешены типы файлов *.jpg, *.png, *.gif</p>
- </div>
- <ul>
- <li id="li_1">
- <label class="description" for="element_1">Загрузить изображение </label>
- <div>
- <input id="element_1" name="element_1" class="element file" type="file"/>
- </div>
- </li>
- <li class="buttons">
- <input type="hidden" name="form_id" value="490795"/>
- <input id="saveForm" class="button_text" type="submit" name="submit"/>
- </li>
- </ul>
- </form>
- <div id="footer">
- Простой скрипт загрузки изображения :)
- </div>
- </div>
- <img id="bottom" src="images/bottom.png" alt="">
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement