从URL到PHP上传的图像功能

时间:2017-06-07 03:54:11

标签: php curl

我看了一遍,似乎无法找到问题的解决方案。任何帮助/指针将不胜感激。

我试图从网址来源提取图片(例如https://scontent.xx.fbcdn.net/v/t1.0-9/17990738_10154386783672539_8144154100714177483_n.jpg?oh=8c1c2fb46e8626f58fd76662f3fe6673&oe=59AAE570

URL来自更大的CURL,我将JSON结果解析为PHP对象数组

我可以提取网址,但是当我尝试将图片信息传递给我的上传功能时,我的检查都失败了(图片尺寸和图像类型),因此无法上传

通过HTML文件上传器手动上传文件时,以下功能有效。以下是通常传递的内容

$file = $_FILES['file'];

这是上传功能

function upload($file)
{
  $target_dir= "../admin/media/";
  $target_file = $target_dir . basename($file['name']);
  $random_number = rand(1000,100000);
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


  //Check that file is not empty
  $check = getimagesize($file['tmp_name']);
  if($check !== false) {
    $uploadOk = 1;
  } else {
    $uploadOk = 0; echo "no image size";
  }

  //Check that file is an image
  if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
      $uploadOk = 0; echo "not an image file";
  }

  $img_info = getimagesize($file['tmp_name']);
  $width = $img_info[0];
  $height = $img_info[1];
  $ratio = $width / $height;

  switch ($img_info[2]) {
    case IMAGETYPE_GIF  : $src = imagecreatefromgif($file['tmp_name']);  break;
    case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($file['tmp_name']); break;
    case IMAGETYPE_PNG  : $src = imagecreatefrompng($file['tmp_name']);  break;
  }

  if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
  // if everything is ok, try to upload file
  } else {
    //UPLOAD ORIGINAL VERSION
    $file_name = $random_number."-".$file['name']."-original";
    $file_name = slugify($file_name);

    $tmp = imagecreatetruecolor($width, $height);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $width, $height, $width, $height);
    //string the target folder and file name together
    $target_file = $target_dir . $file_name;

    //upload image to server
    imagejpeg($tmp, $target_file . ".jpg");
    $file_name_original = $file_name . ".jpg";

    //UPLOAD THUMBNAIL VERSION
    $file_name = $random_number."-".$file['name']."-thumbnail";
    $file_name = slugify($file_name);

    $min_dim_thumbnail = 300;
    if( $ratio > 1) {
            $new_width = $min_dim_thumbnail*$ratio;
            $new_height = $min_dim_thumbnail;
            $x_center = ($new_width/2) - 150;
            $y_center = 0;
        } else {
            $new_width = $min_dim_thumbnail;
            $new_height = $min_dim_thumbnail/$ratio;
            $x_center = 0;
            $y_center = ($new_height/2) - 150;
        }

    $tmp = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    $crop = imagecrop($tmp, ['x' => $x_center, 'y' => $y_center, 'width' => '300', 'height' => '300']);

    $target_file = $target_dir . $file_name;

    imagejpeg($crop, $target_file . ".jpg");
    $file_name_thumbnail = $file_name . ".jpg";


    //UPLOAD BANNER VERSION
    $file_name = $random_number."-".$file['name']."-banner";
    $file_name = slugify($file_name);

    $min_dim_height = 450;
    $min_dim_width = 800;
    if( $ratio > 1.78) {
            $new_width = $min_dim_height*$ratio;
            $new_height = $min_dim_height;
            $x_center = ($new_width/2) - 400;
            $y_center = 0;
        } else {
            $new_width = $min_dim_width;
            $new_height = $min_dim_width/$ratio;
            $x_center = 0;
            $y_center = ($new_height/2) - 225;
        }

    $tmp = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    $crop = imagecrop($tmp, ['x' => $x_center, 'y' => $y_center, 'width' => '800', 'height' => '450']);

    $target_file = $target_dir . $file_name;

    imagejpeg($crop, $target_file . ".jpg");
    $file_name_banner = $file_name . ".jpg";


  }
  //return the slugified file name to the calling function
  return array($file_name_original, $file_name_banner, $file_name_thumbnail);
}?>

这里是slugify功能

<?php
//FUNCTION FOR RETURNING SLUG VALUE FROM A STRING
function slugify($text)
{
    // replace non letter or digits by -
    $text = preg_replace('~[^\pL\d]+~u', '-', $text);
    // transliterate
       //$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    // remove unwanted characters
    $text = preg_replace('~[^-\w]+~', '', $text);
    // trim
    $text = trim($text, '-');
    // remove duplicate -
    $text = preg_replace('~-+~', '-', $text);
    // lowercase
    $text = strtolower($text);

    if (empty($text)) {
        return 'n-a';
    }
    return $text;
}?>

以下是我正在尝试处理上传的单独php文件中使用的代码

    $image_url = $value->{'cover'}->{'source'}; //define image source
    $file = file_get_contents($image_url);
    $file['tmp_name'] = $image_url;
    $file['name'] = basename($files['tmp_name']);
    list($image_original, $image_banner, $image_thumbnail) = upload($file); //pass PHP image to our upload function

提前谢谢。

1 个答案:

答案 0 :(得分:0)

可能你只需分配相同的密钥而不是默认的class UserService { usr: User; //usr: any; (?) } class UserMoreService { getInfo (usr: User) {console.log(usr);} //usr: any (?) } 数组:

$_FILES

我没有尝试过,但理论上它应该有用。