Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Code:
Selected Language:
Show Linenumbers:
Short link for Twitter:
HTML:

HTML view:

Copy Source | Copy HTML
  1. <?define("SM_SAFE_MODE", true); require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
  2.  
  3.     function CustomResizePicture($arFile, $arResize)
  4.     {
  5.         global $APPLICATION;
  6.         $APPLICATION->RestartBuffer();
  7.  
  8.  
  9.         if(strlen($arFile["tmp_name"]) <=  0)
  10.             return $arFile;
  11.  
  12.         if(array_key_exists("error", $arFile) && $arFile["error"] !==  0)
  13.             return GetMessage("IBLOCK_BAD_FILE_ERROR");
  14.  
  15.         $file = $arFile["tmp_name"];
  16.  
  17.         if(!file_exists($file) && !is_file($file))
  18.             return GetMessage("IBLOCK_BAD_FILE_NOT_FOUND");
  19.  
  20.         $width = intval($arResize["WIDTH"]);
  21.         $height = intval($arResize["HEIGHT"]);
  22.  
  23.  
  24.         if($width <=  0 && $height <=  0)
  25.             return $arFile;
  26.  
  27.         $orig = @getimagesize($file);
  28.         if(!is_array($orig))
  29.             return GetMessage("IBLOCK_BAD_FILE_NOT_PICTURE");
  30.  
  31.         if(($width >  0 ) || ($height >  0 ))
  32.         {
  33.             $width_orig = $orig[ 0];
  34.             $height_orig = $orig[1];
  35.  
  36.             if($width <=  0)
  37.             {
  38.                 $width = $width_orig;
  39.             }
  40.  
  41.             if($height <=  0)
  42.             {
  43.                 $height = $height_orig;
  44.             }
  45.             //if($width_orig > $width)
  46.             //{
  47.             //    $height = ($width / $width_orig) * $height_orig;
  48.             //}
  49.  
  50.             //if($height_orig > $height)
  51.             //{
  52.             //    $width = ($height / $height_orig) * $width_orig;
  53.             //}
  54.  
  55.             $height_1 = ($width / $width_orig) * $height_orig;
  56.             $width_1 = ($height_1 / $height_orig) * $width_orig;
  57.  
  58.  
  59.             $width_2 = ($height / $height_orig) * $width_orig;
  60.             $height_2 = ($width_2 / $width_orig) * $height_orig;
  61.  
  62.  
  63.             //echo 'h:'.$height_1.' w:'.$width_1.'<br />';
  64.             //echo 'h:'.$height_2.' w:'.$width_2;
  65.  
  66.             if( $height_1<=$height && $width_1<=$width)
  67.             { $height = $height_1; $width = $width_1; }
  68.             else
  69.             { $height = $height_2; $width = $width_2; }
  70.  
  71.             //die();
  72.  
  73.  
  74.             $image_type = $orig[2];
  75.             if($image_type == IMAGETYPE_JPEG)
  76.                 $image = imagecreatefromjpeg($file);
  77.             elseif($image_type == IMAGETYPE_GIF)
  78.                 $image = imagecreatefromgif($file);
  79.             elseif($image_type == IMAGETYPE_PNG)
  80.                 $image = imagecreatefrompng($file);
  81.             else
  82.                 return false/*GetMessage("IBLOCK_BAD_FILE_UNSUPPORTED")*/;
  83.  
  84.             $image_p = imagecreatetruecolor($width, $height);
  85.  
  86.  
  87.             if($image_type == IMAGETYPE_JPEG)
  88.             {
  89.  
  90.                     header("Content-type: image/jpeg");
  91.                     header("Pragma: public");//может ÐºÐµÑˆÐ¸Ñ€Ð¾Ð²Ð°Ñ‚ься Ð²ÑÐµÐ¼Ð¸ (прокси-серверами Ñ‚оже)
  92.                     header("Cache-Control: max-age=3600");//закешировать Ð½Ð° ÑÑ‚о Ð²Ñ€ÐµÐ¼Ñ
  93.  
  94.  
  95.                 if($arResize["METHOD"] === "resample")
  96.                     imagecopyresampled($image_p, $image,  0,  0,  0,  0, $width, $height, $width_orig, $height_orig);
  97.                 else
  98.                     imagecopyresized($image_p, $image,  0,  0,  0,  0, $width, $height, $width_orig, $height_orig);
  99.  
  100.                 if($arResize["COMPRESSION"] >  0)
  101.                     imagejpeg($image_p, false, $arResize["COMPRESSION"]);
  102.                 else
  103.                     imagejpeg($image_p/*, $file*/);
  104.             }
  105.             elseif($image_type == IMAGETYPE_GIF && function_exists("imagegif"))
  106.             {
  107.  
  108.                     header("Content-type: image/gif");
  109.                     header("Pragma: public");//может ÐºÐµÑˆÐ¸Ñ€Ð¾Ð²Ð°Ñ‚ься Ð²ÑÐµÐ¼Ð¸ (прокси-серверами Ñ‚оже)
  110.                     header("Cache-Control: max-age=3600");//закешировать Ð½Ð° ÑÑ‚о Ð²Ñ€ÐµÐ¼Ñ
  111.  
  112.  
  113.                 imagetruecolortopalette($image_p, true, imagecolorstotal($image));
  114.                 imagepalettecopy($image_p, $image);
  115.  
  116.                 //Save transparency for GIFs
  117.                 $transparentcolor = imagecolortransparent($image);
  118.                 if($transparentcolor >=  0 && $transparentcolor < imagecolorstotal($image))
  119.                 {
  120.                     $transparentcolor = imagecolortransparent($image_p, $transparentcolor);
  121.                     imagefilledrectangle($image_p,  0,  0, $width, $height, $transparentcolor);
  122.                 }
  123.  
  124.                 if($arResize["METHOD"] === "resample")
  125.                     imagecopyresampled($image_p, $image,  0,  0,  0,  0, $width, $height, $width_orig, $height_orig);
  126.                 else
  127.                     imagecopyresized($image_p, $image,  0,  0,  0,  0, $width, $height, $width_orig, $height_orig);
  128.                 imagegif($image_p/*, $file*/);
  129.             }
  130.             else
  131.             {
  132.  
  133.                     header("Content-type: image/png");
  134.                     header("Pragma: public");//может ÐºÐµÑˆÐ¸Ñ€Ð¾Ð²Ð°Ñ‚ься Ð²ÑÐµÐ¼Ð¸ (прокси-серверами Ñ‚оже)
  135.                     header("Cache-Control: max-age=3600");//закешировать Ð½Ð° ÑÑ‚о Ð²Ñ€ÐµÐ¼Ñ
  136.  
  137.  
  138.                 //Save transparency for PNG
  139.                 $transparentcolor = imagecolorallocate($image_p,  0,  0,  0);
  140.                 $transparentcolor = imagecolortransparent($image_p, $transparentcolor);
  141.                 imagealphablending($image_p, false);
  142.                 imagesavealpha($image_p, true);
  143.  
  144.                 if($arResize["METHOD"] === "resample")
  145.                     imagecopyresampled($image_p, $image,  0,  0,  0,  0, $width, $height, $width_orig, $height_orig);
  146.                 else
  147.                     imagecopyresized($image_p, $image,  0,  0,  0,  0, $width, $height, $width_orig, $height_orig);
  148.  
  149.                 imagepng($image_p/*, $file*/);
  150.             }
  151.  
  152.  
  153.  
  154.  
  155.             imagedestroy($image);
  156.             imagedestroy($image_p);
  157.  
  158.             /*$arFile["size"] = filesize($file);*/
  159.             return $arFile;
  160.         }
  161.  
  162.     }
  163.  
  164.     $width = intval( $_GET['w'] );
  165.     $height = intval( $_GET['h'] );
  166.     $image_id = intval( $_GET['i'] );
  167.  
  168.     CustomResizePicture( CFile::MakeFileArray($image_id), array(
  169.                                           "WIDTH" => $width,
  170.                                           "HEIGHT" => $height,
  171.                                           "METHOD" => "resample",
  172.                                           'COMPRESSION' => '100',
  173.                                       )
  174.     );
  175.  
  176.  
  177. require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_after.php");?>




Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2012