Best PHP Image Crop Class

前端 未结 9 1282
盖世英雄少女心
盖世英雄少女心 2021-01-30 14:30

I\'m designing a website and I need to:

  • Upload the image
  • Validate that it\'s an image (and not, oh I don\'t know.... a virus :) )
  • Resize the Imag
9条回答
  •  感动是毒
    2021-01-30 15:10

    My personal favorite Image Manipulation Library is WideImage. It makes is ridiculously easy to do that kind of task.

    WideImage::load('pic.png')
    ->crop('center', 'center', 90, 50)->saveToFile('cropped/pic.jpg');
    

    As for validating if it is actually an image or not, use finfo or PEAR::Mime_type. I personally prefer PEAR::Mime_Type. It uses finfo but it's just simpler to use.

    Using finfo:

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mimetype = finfo_file($finfo, $filename);
    
    $isImage = (preg_match('#^image/#', $mimetype) === 1);
    

    Using PEAR::Mime_Type:

    $mimetype = MIME_Type::autoDetect($filename);
    
    $isImage = MIME_Type::wildcardMatch('image/*', $mimetype);
    

提交回复
热议问题