我在從網路編輯影像時遇到問題。
主要目標是從 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 中嘗試了幾件關于影像處理的事情,但我仍在苦苦掙扎。任何指南?自我提醒:我應該在某處進行檢查。
uj5u.com熱心網友回復:
這是執行您想要的功能腳本的示例:
<?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');
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/450086.html
