Jump to content

Image not saved… HELP!


Noel Boss
 Share

Recommended Posts

Hi, I'm stuck since hours and don't know what to do. Here is my Problem:

I try to generate Previews of PDF using imagick. I have 4 PDF, I generate a preview of the first page of the pdf, save it to a temporary file and want to import it using the api into an image field. It works for the later 3 pdf but not the first. I add it to the image field and save it. Inside the function that saves it, the image is stored in 'data' as well as in 'itemsAdded' but as soon as i leave the function, its nowhere to be found.

Process:

1. create previewimage using imagick and create Pageimage > works

2. add image to filed 'filepreviews', returns Pageimages array with image added > okay

3. save page > returns true

4. Outside renderPreview method, image is not anymore in 'filepreviews'

// mymodule
// …
// foreach($files as $file){

  $preview = wire('page')->filepreviews->get('name*='.$file->basename(false));
  // if there is no preview image…
  if (!$preview instanceof Pageimage) {
	$this->renderPreview($file) // we create one using $this->renderPreview > should return true on successful save
	dump(wire('page')->filepreviews); // my Image is nowhere to be found
  	$preview = wire('page')->filepreviews->get('name*='.$file->basename(false));
  }

// end for each

	// render a preview of an otherwhise not supported file format
	// return true if sucessfull save
	private function renderPreview($file)
	{
		$page = wire('page');
		
		// get path to temporary image
		$tempFile = $path.$file->basename(false).'-preview.jpg';
		// … some imagick code
		// … and save it:
		$imagick->writeImage($tempFile);

		$img = new Pageimage($page->filepreviews, $tempFile);
		$img->description = $file->basename(false);

		// destroy temp image
		unlink($tempFile);

		// this is my Pageimage, all good…
		dump($img);

		// save image, my Pageimage can be found in data and itemsAdded – all good
		dump($page->filepreviews->add($img));
		$page->of(false);
		$success = $page->save();
	
		// sucess = true
		dump($success);

		return $success;
	}

5acf2824c13fa_ScreenShot2018-04-12at11_27_59.thumb.png.ceb8f022b9c411004e767b25abd26a02.png

 

Second question:

Would there a generally better approach? Like using pageFiles somehow. Goal is to be able to use the image api like scale etc – I don't generally need the images to be stored in an image field.

 

Link to comment
Share on other sites

Have you tried to do $page->of(false) before you add the image?
 

$page->of(false);
$page->filepreviews->add($img);
$success = $page->save();

EDIT: this would explain why the first did not get added but the follwing did. Because of() was only set to false after adding the first image

  • Like 6
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...