We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40653
    • 2 Posts
    Hello! I try to write a plugin which will resize big images while uploading via File Manager (OnFileManagerUpload event). There is it's code:

    <?php
    // Options
    $ext_pics = array(".jpg", ".jpeg", ".gif", ".png");
    $max_x="1000";
    $max_y="800";
    
    // Change picture`s pixel size 
    function create_small($name_big,$name_small,$max_x="800", $max_y="600")
    {
      list($x, $y, $t, $attr) = getimagesize($name_big);
      if ($x > 1000)
      {
        if ($t == IMAGETYPE_GIF)
          $big=imagecreatefromgif($name_big);
        else if ($t == IMAGETYPE_JPEG)
          $big=imagecreatefromjpeg($name_big);
        else if ($t == IMAGETYPE_PNG)
          $big=imagecreatefrompng($name_big); 
        else 
          return;    
        
        if ($x > $y)
        {
          $xs=$max_x;
          $ys=$max_x/($x/$y);
        }
        else 
        {
          $ys=$max_y;
          $xs=$max_y/($y/$x);
        }
        $small=imagecreatetruecolor ($xs,$ys);
        $res = imagecopyresampled($small,$big,0,0,0,0,$xs,$ys,$x,$y);
        imagedestroy($big);
        imagejpeg($small,$name_small);
        imagedestroy($small);
      }       
    }
    
    $currentdoc = $modx->newObject('modResource');
    foreach($files as $file)
    {
        $full_filename=$file['name'];
        foreach($ext_pics as $ext_pic)
        {
          $pos = strpos($full_filename, $ext_pic);
          if ($pos === false) {
            // not picture  
          } else {
            if($file['error'] == 0)
            {
              $pathInfo = pathinfo($file['name']);
              $oldPath = $directory.$file['name'];
              $final_name=$pathInfo['filename']).substr(md5(time()), 11).'-'.date("Y-m-d-h-s");
              $newPath = $currentdoc->cleanAlias($final_name.'.'.$pathInfo['extension'];
              $source->renameObject($oldPath, $newPath);
              create_small($newPath,$newPath,$max_x,$max_y);
            }
          }  
        }    
    }
    
          
    ?>
    


    While uploading I get an error. What problems do I have in this code?

    * sorry for my English [ed. note: indexmsk last edited this post 14 years, 2 months ago.]