图片从URL到PHP上传函数

uurv41yg  于 2023-11-16  发布在  PHP
关注(0)|答案(2)|浏览(93)

我已经找遍了,似乎找不到解决我的问题的办法。任何帮助/指针将不胜感激。
我正在尝试从URL源(即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


先谢谢你了。

jchrr9hc

jchrr9hc1#

很可能你只需要分配相同的键,而不是默认的$_FILES数组:

# Create the two elements that are required for the function
$files['tmp_name'] = 'https://s-media-cache-ak0.pinimg.com/736x/c6/4c/e0/c64ce05bf01ccb3ea8af44de5980cbe4.jpg';
$files['name'] = basename($files['tmp_name']);
# Do the regular upload
upload($files);

字符串
我没试过,但理论上应该可以。

qf9go6mv

qf9go6mv2#

我希望这会有所帮助:

$url = 'https://v2.convertapi.com/d/kymp602xvk3qvuwgtst0gllvvc9033tl/image.svg';

$path_parts = pathinfo($url);
$_FILES = [];
$_FILES['file']["name"] = $path_parts['filename'];
$_FILES['file']["tmp_name"] =  $url;
$_FILES['file']["extension"] =  $path_parts['extension'];

echo "<pre>";
    print_r($path_parts);
echo "</pre>";

echo "<pre>";
print_r($_FILES);
echo "</pre>";

字符串

相关问题