如何从 URL 获取图像,即时编辑并保存
How to get image from URL, edit on the fly and save
我在编辑来自网络的图像时遇到问题。
主要目标是从 url 中获取图像,在周围添加一些填充并保存它。
//get image
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $image_url); //get result from $image_url
$image_raw = curl_exec($ch); //get image
curl_close($ch);
file_put_contents( 'mysavelocation', $image_raw ); //image saved. this is working fine
//add padding
$orig_img = imagecreatefromstring($image_raw); //get raw image from above. Probably this the part that is not working, because in my understanding, $image_raw should be in different format (?).
list($orig_w, $orig_h) = getimagesize($orig_img); //get image size to add padding later.
echo $orig_w.','.$orig_h; //this is empty
$image_w_padding = imagecreatetruecolor( ($orig_w+20), ($orig_h+20) ); // create new image with 10px padding
imagecopyresampled($image_w_padding, $orig_img, 10, 10, 0, 0, ($orig_w+20), ($orig_h+20), $orig_w, $orig_h); //copy original to new with paddings
imagejpeg($image_w_padding, 'mysavelocation2', 84); //save with jpg quality
PS:我最近在 PHP 尝试了一些关于图像处理的事情,但我仍在挣扎。任何指南?
自我提醒:我应该在某处支票。
这里是一个功能脚本的例子,它可以做你想做的事:
<?php
$padding_size = 20;
$padding_color = 0xff0000;
$url = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Cat_poster_1.jpg/390px-Cat_poster_1.jpg";
$data = file_get_contents($url);
$image = imagecreatefromstring($data);
$width = imagesx($image);
$height = imagesy($image);
$padded_image = imagecreatetruecolor($width + 2*$padding_size, $height + 2*$padding_size);
imagefill($padded_image, 0, 0, $padding_color);
imagecopy($padded_image, $image, $padding_size, $padding_size, 0, 0, $width, $height);
imagepng($padded_image, 'padded.png');
输出:
我在编辑来自网络的图像时遇到问题。
主要目标是从 url 中获取图像,在周围添加一些填充并保存它。
//get image
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $image_url); //get result from $image_url
$image_raw = curl_exec($ch); //get image
curl_close($ch);
file_put_contents( 'mysavelocation', $image_raw ); //image saved. this is working fine
//add padding
$orig_img = imagecreatefromstring($image_raw); //get raw image from above. Probably this the part that is not working, because in my understanding, $image_raw should be in different format (?).
list($orig_w, $orig_h) = getimagesize($orig_img); //get image size to add padding later.
echo $orig_w.','.$orig_h; //this is empty
$image_w_padding = imagecreatetruecolor( ($orig_w+20), ($orig_h+20) ); // create new image with 10px padding
imagecopyresampled($image_w_padding, $orig_img, 10, 10, 0, 0, ($orig_w+20), ($orig_h+20), $orig_w, $orig_h); //copy original to new with paddings
imagejpeg($image_w_padding, 'mysavelocation2', 84); //save with jpg quality
PS:我最近在 PHP 尝试了一些关于图像处理的事情,但我仍在挣扎。任何指南? 自我提醒:我应该在某处支票。
这里是一个功能脚本的例子,它可以做你想做的事:
<?php
$padding_size = 20;
$padding_color = 0xff0000;
$url = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Cat_poster_1.jpg/390px-Cat_poster_1.jpg";
$data = file_get_contents($url);
$image = imagecreatefromstring($data);
$width = imagesx($image);
$height = imagesy($image);
$padded_image = imagecreatetruecolor($width + 2*$padding_size, $height + 2*$padding_size);
imagefill($padded_image, 0, 0, $padding_color);
imagecopy($padded_image, $image, $padding_size, $padding_size, 0, 0, $width, $height);
imagepng($padded_image, 'padded.png');
输出: