图片从URL到PHP上传功能
我已经找遍了,似乎找不到解决我问题的办法。如有任何帮助/建议,将不胜感激 我正试图从URL源中提取图像(即) URL来自一个更大的卷曲,我正在将JSON结果解析到一个PHP对象数组中 我可以拉url,但当我尝试将图像信息传递给我的上载功能时,它无法通过我的两项检查(图像大小和图像类型),因此无法上载 当通过HTML文件上传器手动上传文件时,以下功能起作用。下面是典型的passd图片从URL到PHP上传功能,php,curl,Php,Curl,我已经找遍了,似乎找不到解决我问题的办法。如有任何帮助/建议,将不胜感激 我正试图从URL源中提取图像(即) URL来自一个更大的卷曲,我正在将JSON结果解析到一个PHP对象数组中 我可以拉url,但当我尝试将图像信息传递给我的上载功能时,它无法通过我的两项检查(图像大小和图像类型),因此无法上载 当通过HTML文件上传器手动上传文件时,以下功能起作用。下面是典型的passd $file = $_FILES['file']; 这是上传器函数 function upload($file) {
$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);
}?>
这是slagify函数
<?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;
}?>
提前谢谢。您可能只需要分配相同的密钥,而不是默认的
$\u文件
数组:
# 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);
我还没有试过,但理论上它应该会起作用。你需要下载这个文件,然后将它传递到上传方法。完全公开:我仍然是一个超级傻瓜。你能给我一个你的意思的代码示例吗?不起作用。。。我在帖子底部添加了我遇到问题的代码。(添加了您的建议)。我没有其他函数,因此无法对其进行测试,但该文件在gdlib中是有效的,因此它应该可以正常工作(取决于
slagify()
的功能)。我更新了我的帖子,以包含slagify()函数。我还将链接示例更新为从cURL结果中提取的链接示例。我注意到URL的构成有点不同,我不确定这是否是原因。再次感谢!
# 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);