Advertisement
s-a--m

Простой скрипт загрузки изображения :)

Sep 27th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.26 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Скрипт резайза и загрузки изображения на сервер
  4.  */
  5. ini_set("display_errors", "1");
  6. ini_set("display_startup_errors", "1");
  7. ini_set('error_reporting', E_ALL | E_STRICT);
  8. error_reporting(E_ALL | E_STRICT);
  9. ini_set('display_errors', 'On');
  10. $SCRIPT_NAME = 'ava.php';
  11. if (isset($_SERVER['SCRIPT_NAME'])) {
  12.     $SCRIPT_NAME = $_SERVER['SCRIPT_NAME'];
  13. }
  14. $folderVars  = array(
  15.     'uploadFolder'    => "./upl/",
  16.     's_ImgFolder'     => "./upl/small/",
  17.     'small_ImgPrefix' => ''
  18. );
  19. $galleryVars = array(
  20.     'new_imgWidth' => 200,
  21.     's_ImgWidth'   => 100,
  22.     's_ImgHeight'  => 100,
  23.     'ImgExts'      => array('gif', 'jpg', 'png')
  24. );
  25. $answer = '';
  26.  
  27. /**
  28.  * @param $w1
  29.  * @param $h1
  30.  * @param $w2
  31.  * @param $h2
  32.  * @param bool $crop
  33.  *
  34.  * @return array
  35.  */
  36. function Tools_getimgwhr($w1, $h1, $w2, $h2, $crop = false) {
  37.     $res = array('w2'=> 0, 'h2'=> 0, 'R1'=> 0, 'R2'=> 0);
  38.     $R1  = $h1 / $w1;
  39.     if ($crop) {
  40.         $R2 = $h2 / $w2;
  41.     } else {
  42.         $R2 = $R1;
  43.         $h2 = $w2 * $R1;
  44.     }
  45.     $res['w2'] = $w2;
  46.     $res['h2'] = $h2;
  47.     $res['R1'] = $R1;
  48.     $res['R2'] = $R2;
  49.     return $res;
  50. }
  51.  
  52. function io_writeImage($type, $filename, $img) {
  53.     $result = false;
  54.     switch ($type) {
  55.     case 'jpg':
  56.         $result = imagejpeg($img, $filename);
  57.         break;
  58.     case 'gif':
  59.         $result = imagegif($img, $filename);
  60.         break;
  61.     case 'png':
  62.         $result = imagepng($img, $filename);
  63.         break;
  64.     }
  65.     return $result;
  66. }
  67.  
  68. function io_readImage($i_type, $filename) {
  69.     $res = false;
  70.     //echo $filename, ' trying read ....';
  71.     switch ($i_type) {
  72.     case 'jpg':
  73.         //echo 'jpg ';
  74.         $res = imagecreatefromjpeg($filename);
  75.         //var_dump($res);
  76.         break;
  77.     case 'gif':
  78.         //echo 'gif ';
  79.         $res = imagecreatefromgif($filename);
  80.         break;
  81.     case 'png':
  82.         //echo 'png ';
  83.         $res = imagecreatefrompng($filename);
  84.         break;
  85.     }
  86.     return $res;
  87. }
  88.  
  89. function tools_imageResize
  90. (
  91.     $type, $img1,
  92.     $w1, $h1,
  93.     $w2, $h2,
  94.     $crop = false
  95. ) {
  96. //    //echo '<br/>';
  97. //    //echo 'begin resizing ',$img1,' from ',$w1,' x ',$h1,' to ',$w2,' x ',$h2,' crop = ',$crop,'<br/>';
  98.     $srcH = $srcW = $srcX = $srcY = 0;
  99.     //echo 'src image ', $img1, ' opening...';
  100.     if (is_file($img1)) {
  101.         //echo 'is file<br/>';
  102.     } else {
  103.         //echo 'is not file<br/>';
  104.     }
  105.     $srcImg = io_readImage($type, $img1);
  106.     //var_dump($srcImg);
  107. //    //echo '<br/>';
  108.     $imgWHR = tools_getImgWHR($w1, $h1, $w2, $h2, $crop);
  109.     //echo 'begint calc resizing from ', $w1, ' x ' . $h1, ' to ', $w2, ' x ', $h2, '<hr/>';
  110.     $R1 = $imgWHR['R1'];
  111.     $R2 = $imgWHR['R2'];
  112.     $h2 = $imgWHR['h2'];
  113.     $w2 = $imgWHR['w2'];
  114.     if ($R1 < $R2) {
  115.         $srcY = 0;
  116.         $srcX = floor(($w1 - $h1 / $R2) / 2);
  117.         $srcW = $h1 / $R2;
  118.         $srcH = $h1;
  119.     } elseif ($R1 > $R2) {
  120.         $srcX = 0;
  121.         $srcY = floor(($h1 - $w1 * $R2) / 2);
  122.         $srcW = $w1;
  123.         $srcH = $w1 * $R2;
  124.     } elseif ($R1 == $R2) {
  125.         $srcX = 0;
  126.         $srcY = 0;
  127.         $srcH = $h1;
  128.         $srcW = $w1;
  129.     }
  130.  
  131.     //echo 'calculated resizing ', $img1, ' from ', $srcW, ' x ', $srcH, ' to ', $w2, ' x ', $h2, ' crop = ', $crop, '<br/>';
  132.     //echo'srcX:Y = ', $srcX, ' : ', $srcY, '<br/>';
  133.     //echo 'src w x h = ', $srcW, ' x ', $srcH, '<br/>';
  134.     //echo 'w2 x h2  = ', $w2, ' x ', $h2;
  135.     $dstImg = imagecreatetruecolor($w2, $h2);
  136.     $r4     = imagecopyresampled($dstImg, $srcImg,
  137.         0, 0,
  138.         $srcX, $srcY,
  139.         $w2, $h2,
  140.         $srcW, $srcH);
  141.     //echo '<br/>im copy resampled returns ', $r4, '<br/>';
  142.     imagedestroy($srcImg);
  143.     $result['img']    = $dstImg;
  144.     $result['width']  = $w2;
  145.     $result['height'] = $h2;
  146.     return $result;
  147. }
  148.  
  149. function io_chkFolders($folderVars) {
  150.     $r1           = $r2 = true;
  151.     $uploadFolder = $folderVars['uploadFolder'];
  152.     $s_ImgFolder  = $folderVars['s_ImgFolder'];
  153.     if (!is_dir($uploadFolder)) {
  154.         $r1 = mkdir($uploadFolder, 0777, true);
  155.     }
  156.     if (!is_dir($s_ImgFolder)) {
  157.         $r2 = mkdir($s_ImgFolder, 0777, true);
  158.     }
  159.     return $r1 && $r2;
  160. }
  161.  
  162. function tools_getFileName($fileId, $ext, $folderVars, $_s = false) {
  163.     $uploadFolder    = $folderVars['uploadFolder'];
  164.     $s_ImgFolder     = $folderVars['s_ImgFolder'];
  165.     $small_ImgPrefix = $folderVars['small_ImgPrefix'];
  166.     if (!$_s) {
  167.         $result = $uploadFolder . $fileId . '.' . $ext;
  168.     } else {
  169.         $result = $s_ImgFolder . $small_ImgPrefix . $fileId . '.' . $ext;
  170.     }
  171.     return $result;
  172. }
  173.  
  174.  
  175. /**
  176.  * @param string $fileId      filename ot id for database
  177.  * @param string $sTmpName    name of uploaded but not moved
  178.  * @param string $sExt        file exstension
  179.  * @param int    $ImgWidth    image width
  180.  * @param int    $ImgHeight   img height
  181.  * @param mixed  $folderVars  see up
  182.  * @param mixed  $galleryVars see up
  183.  *
  184.  * @return mixed
  185.  *
  186.  */
  187.  
  188. function Io_addimage
  189. (
  190.     $fileId, $sTmpName,
  191.     $sExt, $ImgWidth, $ImgHeight,
  192.     $folderVars, $galleryVars
  193. ) {
  194.     $new_imgWidth = $galleryVars['new_imgWidth'];
  195.     $s_ImgWidth   = $galleryVars['s_ImgWidth'];
  196.     $s_ImgHeight  = $galleryVars['s_ImgHeight'];
  197.  
  198.     $sNewName      = tools_getFileName($fileId, $sExt, $folderVars);
  199.     $s_ImgFilename = tools_getFileName($fileId, $sExt, $folderVars, true);
  200.     //echo '<hr/>New Name is ', $sNewName, ' new small img name is ', $s_ImgFilename, '<hr/>';
  201.     $upl_moved = false;
  202.     $r1        = io_chkFolders($folderVars);
  203.     //echo 'folders check...', $r1, '<br/>';
  204.  
  205.     if ($ImgWidth > $new_imgWidth) {
  206.         //echo '$ImgWidth > ', $new_imgWidth, '<br/>';
  207.         $resize = tools_imageResize(
  208.             $sExt, $sTmpName, $ImgWidth, $ImgHeight, $new_imgWidth, $ImgHeight
  209.         );
  210.         //echo 'resize result: <br/>';
  211.         //var_dump($resize);
  212.         //echo '<br/>';
  213.         $new_Img   = $resize['img'];
  214.         $big_width     = $resize['width'];
  215.         $big_height    = $resize['height'];
  216.         $ImgHeight = $big_height;
  217.         $ImgWidth  = $big_width;
  218.         //echo 'Resing complete, w x h = ', $width, ' x ', $height, '<br/>';
  219.         $r2 = io_writeImage($sExt, $sNewName, $new_Img);
  220.         //echo 'trying to write ...', $r2, '<br/>';
  221.         imagedestroy($new_Img);
  222.         //echo '$resize  width ', $width, ' height ', $height, ' $result ', $r2;
  223.     } else {
  224.         //echo 'img width < ', $new_imgWidth, '<br/>';
  225.         //echo $sTmpName, '<br/>';
  226.         $upl_moved
  227.             = $r2 = move_uploaded_file($sTmpName, $sNewName);
  228.         //echo $sNewName, ' writing big image...', $r2, '<br/>';
  229.     }
  230.     chmod($sNewName, 0777);
  231.     //echo '<hr/>small IMAGE<hr/>';
  232.     if (($ImgWidth > $s_ImgWidth) && ($ImgHeight > $s_ImgHeight)) {
  233.         //echo "($ImgWidth > $s_ImgWidth) && ($ImgHeight) > $s_ImgHeight";
  234.         //echo '<hr/>';
  235.         $resize = tools_imageResize(
  236.             $sExt, $sNewName, $ImgWidth, $ImgHeight, $s_ImgWidth, $s_ImgHeight, true
  237.         );
  238.         $s_Img  = $resize['img'];
  239.         $small_width  = $resize['width'];
  240.         $small_height = $resize['height'];
  241.         $s_ImgWidth = $small_width;
  242.         $s_ImgHeight = $small_height;
  243.         //var_dump($resize);
  244.         $r3 = io_writeImage($sExt, $s_ImgFilename, $s_Img);
  245.         //echo 'io_writeImage writeing small image...', $r3, '<br/>';
  246.         imagedestroy($s_Img);
  247.     } else {
  248.         //echo "($ImgWidth < $s_ImgWidth) ||($ImgHeight) < $s_ImgHeight";
  249.         //echo '<hr/>';
  250.         $s_ImgWidth  = $ImgWidth;
  251.         $s_ImgHeight = $ImgHeight;
  252.         if ($upl_moved) {
  253.             //echo 'already uploaded, copying...';
  254.             $r3 = copy($sNewName, $s_ImgFilename);
  255.             //echo $r3, '<br/>';
  256.  
  257.         } else {
  258.             //echo $sTmpName, '<br/>';
  259.             $r3 = move_uploaded_file($sTmpName, $s_ImgFilename);
  260.             //echo $s_ImgFilename, ' writing small image...', $r3, '<br/>';
  261.         }
  262.     }
  263.     chmod($s_ImgFilename, 0777);
  264.     /* $imgRes['color_list'] = getColorsList($sExt,$sNewName,$ImgWidth,$ImgHeight);*/
  265.     $imgRes['width'] = $ImgWidth;
  266.     $imgRes['height'] = $ImgHeight;
  267.     $imgRes['small_width'] = $s_ImgWidth;
  268.     $imgRes['small_height'] = $s_ImgHeight;
  269.  
  270.     $imgRes['success'] = $r1 && $r2 && $r3;
  271.     //echo 'success = ', $imgRes['success'], '<hr/>';
  272.     return $imgRes;
  273. }
  274.  
  275. /**
  276.  * @param string $filename
  277.  *
  278.  * @return array|bool
  279.  *
  280.  */
  281.  
  282. function Tools_imageinfo
  283. (
  284.     $filename = 'null'
  285. ) {
  286.     if (!is_file($filename)) {
  287.         //var_dump($filename);
  288.         //echo $filename, ' is not a file. ';
  289.         return false;
  290.     }
  291.     if (!$data = getimagesize($filename) or !$filesize = filesize($filename)) {
  292.         //echo 'file size error ';
  293.         return false;
  294.     }
  295.     $extensions = array(
  296.         1  => 'gif',
  297.         2  => 'jpg',
  298.         3  => 'png',
  299.         4  => 'swf',
  300.         5  => 'psd',
  301.         6  => 'bmp',
  302.         7  => 'tiff',
  303.         8  => 'tiff',
  304.         9  => 'jpc',
  305.         10 => 'jp2',
  306.         11 => 'jpx',
  307.         12 => 'jb2',
  308.         13 => 'swc',
  309.         14 => 'iff',
  310.         15 => 'wbmp',
  311.         16 => 'xbmp'
  312.     );
  313.     $result     = array(
  314.         'width'  => $data[0],
  315.         'height' => $data[1],
  316.         'ext'    => $extensions[$data[2]],
  317.         'size'   => $filesize,
  318.         'mime'   => $data['mime']);
  319.     return $result;
  320. }
  321.  
  322. /**
  323.  * @param int $bytes     bytes
  324.  * @param int $precision precision
  325.  *
  326.  * @return string
  327.  *
  328.  */
  329.  
  330. function Tools_bytestosize1024($bytes, $precision = 2) {
  331.     $u = array('B', 'KB', 'MB');
  332.     $i = (int)floor(log($bytes, 1024));
  333.     return round($bytes / pow(1024, $i), $precision) . ' ' . $u[$i];
  334. }
  335.  
  336. if (isset($_FILES['element_1'])) {
  337.  
  338.     $sTmpName  = explode(".", $_FILES['element_1']['name']);
  339.     $sFileName = $sTmpName[0];
  340.     $sTmpName  = $_FILES['element_1']['tmp_name'];
  341.     $ImgExts   = $galleryVars['ImgExts'];
  342.     $imageInfo = tools_imageInfo($sTmpName);
  343.     if (!$imageInfo
  344.         or !in_array($imageInfo['ext'], $ImgExts)
  345.     ) {
  346.         die('Неподдерживаемый формат изображения. ');
  347.     }
  348.     $sExt      = $imageInfo['ext'];
  349.     $ImgWidth  = $imageInfo['width'];
  350.     $ImgHeight = $imageInfo['height'];
  351.     $sFileSize = $imageInfo['size'];
  352.  
  353.     $img_res = io_addImage(
  354.         $sFileName, $sTmpName,
  355.         $sExt, $ImgWidth, $ImgHeight,
  356.         $folderVars, $galleryVars
  357.     );
  358.  
  359.  
  360.  
  361.     $new_width = $img_res['width'];
  362.     $new_height = $img_res['height'];
  363.     $small_width = $img_res['small_width'];
  364.     $small_height = $img_res['small_height'];
  365.  
  366.     //echo '<hr/>hi';
  367.     //echo '<pre>io_addImage<br/>';
  368.     //var_dump($Img_res2);
  369.     //echo'</pre>';
  370.     //echo '<hr/>';
  371.     $sFileSize               = tools_bytesToSize1024($sFileSize, 1);
  372.     $uploaded_filename       = tools_getFileName($sFileName, $sExt, $folderVars, false);
  373.     $uploaded_small_filename = tools_getFileName($sFileName, $sExt, $folderVars, true);
  374.  
  375.     ob_start(false);
  376.     echo <<<EOF
  377. <div class="s">
  378.     <p>Ваш файл: {$sFileName} передан удачно.</p>
  379.     <p>Оригинальное разрешение: {$ImgWidth} x {$ImgHeight} </p>
  380.     <p>Размер: {$sFileSize}</p>
  381.     <img src="{$uploaded_filename}"></img>
  382.     <p>Разрешение изображения на сервере: {$new_width} x {$new_height} </p>
  383.     <img src="{$uploaded_small_filename}"></img>
  384.     <p>Разрешение уменьшенной копии: {$small_width} x {$small_height} </p>
  385. EOF;
  386.     echo "</div>";
  387.     $answer = ob_get_flush();
  388.     ob_clean();
  389. }
  390. ?>
  391.  
  392. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  393.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  394. <html xmlns="http://www.w3.org/1999/xhtml">
  395. <head>
  396.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  397.     <title>Загрузить изображение</title>
  398.     <link rel="stylesheet" type="text/css" href="./colors/color1/view.css">
  399.  
  400. </head>
  401. <body id="main_body">
  402.  
  403. <img id="top" src="images/top.png" alt="">
  404.  
  405. <div id="form_container">
  406.  
  407.     <h1><a>Загрузить изображение</a></h1>
  408.     <?=$answer;?>
  409.     <form id="form_490795" class="appnitro" enctype="multipart/form-data" method="post"
  410.           action="<?php //echo $SCRIPT_NAME; ?>">
  411.         <div class="form_description">
  412.             <h2>Загрузить изображение</h2>
  413.  
  414.             <p>Выберите файл для загрузки. Разрешены типы файлов *.jpg, *.png, *.gif</p>
  415.         </div>
  416.         <ul>
  417.  
  418.             <li id="li_1">
  419.                 <label class="description" for="element_1">Загрузить изображение </label>
  420.  
  421.                 <div>
  422.                     <input id="element_1" name="element_1" class="element file" type="file"/>
  423.                 </div>
  424.             </li>
  425.  
  426.             <li class="buttons">
  427.                 <input type="hidden" name="form_id" value="490795"/>
  428.                 <input id="saveForm" class="button_text" type="submit" name="submit"/>
  429.             </li>
  430.         </ul>
  431.     </form>
  432.     <div id="footer">
  433.         Простой скрипт загрузки изображения :)
  434.     </div>
  435. </div>
  436. <img id="bottom" src="images/bottom.png" alt="">
  437. </body>
  438. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement