How to save a GD created image to an object in Silverstripe 4 - gd

I am working on a project in Silverstripe 4. On page save/ publish I want to create a "MetaImage" which is made up of random tiles images associated with the page.
The MetaImage is created by GD - I'm then trying to save it into the ORM. Here's some example code:
public function onBeforeWrite()
{
$metaImage = imagecreatetruecolor(1200, 1200);
//GD image created here
$fileName = 'metaimage-'.$this->ID.'.jpeg';
$saveFolder = Folder::find_or_make('MetaImages');
imagejpeg($metaImage, 'assets/MetaImages/'. $fileName);
$metaObjectImage = new Image();
$metaObjectImage->ParentID = $saveFolder->ID;
$metaObjectImage->Filename = $fileName;
$metaObjectImage->write();
$this->MetaImage()->$metaObjectImage;
imagedestroy($metaImage);
parent::onBeforeWrite();
}
All I get is a red box in the "Files" section of the CMS with "File cannot be found".
How do I associate the created GD image with $metaObjectImage?
How can I make it that if "metaimage-6.jpeg" already exists it's replaced by a new copy of the file?

This was my solution using setFromLocalFile:
$saveFolder = Folder::find_or_make('MetaImages');
$fileName = bin2hex(random_bytes(32)).'.jpeg';
$tmpFolder = 'assets/'.$saveFolder->Title;
imagejpeg($metaImage, $tmpFolder.'/'.$fileName);
$metaObjectImage = new Image();
$metaObjectImage->setFromLocalFile($tmpFolder.'/'.$fileName);
$metaObjectImage->ParentID = $saveFolder->ID;
$metaObjectImage->write();
$this->MetaImage()->$metaObjectImage;

Related

how to use database saved links of image to get images from laravel folder

i have saved images in 'public/images' folder in Laravel and the link/name is saved in mysql database. all i want to get all images via api call. the problem is i can get a single image easily but couldnt get all the images.[[[enter image description here](https://i.stack.imgur.com/9gerk.jpg)](https://i.stack.imgur.com/MIBIk.jpg)](https://i.stack.imgur.com/QcPQm.jpg)
i have no issue with single image however unable to get all at once. thanks
Try this instead
Store the only image name and extension into database and then retrieve it by using DB and use asset to get image, refer the following code as practice.
$custom_data = array();
for ($i = 0; i <= $total_images; $i++) {
$image_name = $data[$i]->image;
if ($image_name != "") {
$image_with_path = asset('public/products/') . '/' . $image_name;
} else {
$image_with_path = "";
}
$custom_data['image'] = $image_with_path;
}

Images not showing in update function using BackPack for Laravel

I´m saving images in my crud. The url is being stored in the DB correctly, however the image is not appearing when I try to edit the element. The image shows like this:
I'm using this mutator:
public function setImageAttribute($value)
{
$evidencia = "image";
$disk = "public";
$destination_path = "public/uploads/evidenciaQuejas";
// if the image was erased
if ($value==null) {
// delete the image from disk
\Storage::disk($disk)->delete($this->{$evidencia});
// set null in the database column
$this->attributes[$evidencia] = null;
}
// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = \Image::make($value)->encode('jpg', 90);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the path to the database
$this->attributes[$evidencia] = $destination_path.'/'.$filename;
}
}
Also, the folder that I created in the server to store the images ("public/uploads/evidenciaQuejas") says it has 0 archives inside even though the image is being stored without errors.
Could you help me please?

How to load List<Asset> from url with Flutter multi_image_picker: ^4.7.14

I am using Flutter multi_image_picker: ^4.7.14 to upload selected photos to server.
It stores images List, In my app new insert part is ok, I am getting the selected images and showing them in a gridview and sent them to the server.
But when I try to update my record, I don't understand how to get my record's images and set them to List. I have the url path of my uploaded images for that record. I want to get images and convert them to List because multi_image_picker is only accepting Asset, but there is no obvious explanation in multi_image_picker read me how to do this.
var images = List<Asset>;
for (var i = 0; i < tempList.length; i++) {
log("image link:" + ad_url + tempList[i].imageDownloadUrl);
final ByteData imageData = await NetworkAssetBundle(
Uri.parse(ad_url + tempList[i].imageDownloadUrl))
.load("");
final Uint8List bytes = imageData.buffer.asUint8List();
Image img = Image.memory(bytes);
// in this part I want to convert img to Asset , but I don't know how to do this.
Asset asset = img.... //How can I do this conversion
images.add(asset);
}

Add image in pdf using itext

I'am creating Spring MVC web application.My image is in folder webapp/resources/img/logo.png of my MVC application.How to load image from that folder to pdf.
I tried with this code.But its trowing java.io.FileNotFoundException.
String imageUrl = "webapp/resources/img/logo.png"
logo = Image.getInstance(imageUrl);
Enhanced you find a example how I does it:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("C:\\test.pdf"));
document.open();
Image img = Image.getInstance(ClassLoader.getSystemResource("attention-icon.jpg"));
img.scaleAbsolute(10, 10);
Phrase phrase = new Phrase();
phrase.add(new Chunk(img, 0,0));
document.add(new Paragraph(phrase));
document.close();
I think I your case the file "webapp/resources/img/logo.png" is to relativ. Try to create a File Object to check its location:
File logo = new File("webapp/resources/img/logo.png");
if(! logo.exists()){
LOG.warn("File " + logo.getName() + " not exists");
}

Webmatrix image upload and ImageMagick

I have a simple image upload page which works really well:
WebImage photo = null;
var newFileName = "";
var imagePath = "";
if(IsPost){
using (var bitmap = (Bitmap)Image.FromFile(Server.MapPath("~/" + imagePath))){
using (var newBitmap = new Bitmap(bitmap)){
newBitmap.SetResolution(72f, 72f);
newBitmap.Save("file300.jpg", ImageFormat.Jpeg);
}
}
var image = "UPDATE PropertyInfo SET PrimaryImage = #0 WHERE PropertyID = #1";
db.Execute(image, newFileName, rPropertyId);
}
Now i also want to use ImageMagick to convert any images uploaded using this form to 72dpi. I have the command line i need to do this, but i don't now how to apply it to the upload process?
convert c:\image.jpg -density 72 c:\image.jpg
Should i do this during the upload process, or once the file has already been uploaded to a server. Is there any way of initiating a command prompt from within WebMatrix?
It seems easier to use existing .NET features to manage the DPI, namely with the System.Drawing.Bitmap.SetResolution method
using (var bitmap = (Bitmap)Image.FromFile(Server.MapPath("~/" + imagePath))){
using (var newBitmap = new Bitmap(bitmap)){
newBitmap.SetResolution(72f, 72f);
newBitmap.Save("file300.jpg", ImageFormat.Jpeg);
}
}
However, you can also use the System.Diagnostics.Process.Start method to run .exes via C# code.

Resources