在php中从本地路径获取图像

时间:2016-09-13 06:12:13

标签: php excel image cakephp

我正在使用Excel工作表和使用CakePHP 3.2编写应用程序的批量上传项目。

我有一张带有列的Excel工作表,可以上传图片。

用户有三种选择

  
      
  1. 在批量上传之前将图像上传到预定义的目录,并在单元格中提供图像的名称,图像将自动显示   从路径中选择。
  2.   
  3. 提供图片的网址( http://website/path/image.jpg
  4.   
  5. 如果图像的路径在本地计算机上,则给出它的路径。 例如, C:\ user \ pictures \ image.jpg if windows ,以及    /home/user/picture/image.jpg 如果 linux
  6.   

这就是我正在做的保存图像

$p_image = $objWorksheet->getCellByColumnAndRow(30, $row)->getValue();
if (filter_var($p_image, FILTER_VALIDATE_URL)) {
   // get image from url
   $full_image_path = $p_image;
} else {

    // get image from folder
    $path = Configure::read('media.bulkUpload.pre.product');

    // full path of the image from root directory
    $full_image_path = $path . DS . $p_image;
}
$upload_path = Configure::read('media.upload') . DS . 'files' . DS;

// new name of image
$img_new_name = uniqid('img_').round(microtime(true) * 1000).rand(1,100000);

if ($full_image_path) {

   // generate uuid directory name
   $dir = Text::uuid();
   // create new directory
   mkdir($upload_path.$dir, 0777, true);

   // save file
   try {
     $img = new \abeautifulsite\SimpleImage($full_image_path);

     // save image of original size   
     $img->best_fit(850,1036)->save($upload_path.$dir.'/'.$img_new_name.'.jpg');

    } catch(Exception $e) {
        echo 'Error: '.$e->getMessage();
     }
}

使用url和pre ftp上传的图片上传工作正常。但是,我如何从本地系统的路径获取图像然后保存它们。

1 个答案:

答案 0 :(得分:3)

如果用户没有自己上传图片,则无法执行此操作。您无法从运行php的外部服务器检索客户端文件系统中的文件。

相关问题