我必须使用两个不同的域,一个用于CDN
,另一个用于我的应用,我尝试使用example1
将图像从cdn.example 2
上传到CURL
,工作得很好但我的问题是在保存之前调整图像大小以获得我想要的尺寸。我不知道这是否会在用户网站或CDN网站上完成,而且我也不知道如何在提出之前阅读它。我有一个功能来上传图像后重新调整,但我上传时从不尝试,有人可以帮助我
example.com/upload.php
<?php
$image_file = (isset($_FILES['Image']) ? $_FILES['Image'] : null);
if(!empty($image_file)){
$filename = $image_file['tmp_name'];
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$post_var = array(
'image' => base64_encode($data),
'id' => 100
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://cdn.example.com/saveimage.php');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . upload_cdn_server_key));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_var);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$out = curl_exec($curl);
$err = curl_error($curl);
curl_close ($curl);
$pms = json_decode($out,true);
$url= $pms['data']['link'];
if(!empty($url)){
$image_uploaded = true;
}
}
cdn.example.com/saveimage.php
if(isset($_POST["image"], $_POST["id"])){
$encoded_file = $_POST['image'];
$productid = $_POST['id'];
$filepath = __DIR__ . '/image/gallery/p/';
$filename = md5($productid.date('Y-m-d H:m:s i')).'-1.jpeg';
$uploadeImage = file_put_contents($filepath.$filename, $decoded_file);
}
答案 0 :(得分:0)
我建议您使用piio.co,将图片上传到存储空间后,只需使用<img />
标记中的原始图片网址,图书馆就会自动执行为你调整大小。
您的图片代码如下所示:
<img data-piio="/image/gallery/p/image1.jpg" />
然后,一旦您初始化Piio的脚本,它将自动调整图像大小并从CDN提供。
这是指向docs
的链接如果你想手动完成,那么我建议你使用原生的php函数,这将使用GD库。 例如,如果要调整JPEG大小,可以从以下开始:
list($width, $height) = getimagesize($filepath);
$original = imagecreatefromjpeg($filepath);
$thumb = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($thumb, $original, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
然后您需要将该图像输出到文件或直接从您的php脚本打印。
imagejpeg($thumb, 'filepath/image.jpg');