Noel Boss

Image not saved… HELP!

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.

 

Share this post


Link to post
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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Harmen
      I want to add a few pages to an AsmSelect Page field inside a repeater using the following code:
      $trialsPage = wire("pages")->get(28422); // Get the page $trialsPage->of(false); $newTrial = $ordersPage->trial_repeater_orders->getNewItem(); // Add item to repeater foreach ($selectedProducts as $selectedProduct){ $productPage = $pages->get("template=product, reference=$selectedProduct"); $newTrial->trial_selected_products->add($productPage); } $newTrial->save(); $trialsPage->save(); However, when I check the page where the field is located it doesn't have the new values as expected. The selected pages exist, the field is in the right location, made sure that the output formatting is turned off: $page->of(false); But it still doesn't work with a variable. No matter what I try, it doesn't work.
      It only works when I replace $selectedProduct with a hardcoded string. Am I doing something wrong here?
    • By louisstephens
      So I have been diving into hooks lately, and I am enjoying them thus far. However, I guess I am a bit stumped on how to achieve what I want too. I am trying to set up a hook that would create a new child page when the parent page is saved. However, when you save the parent page a second time, I just need to update the child page without creating multiple child pages. What would be the best way to go about this?
      So after rereading my post, I believe it is a bit vague so I will try to explain more. 
      The Goal:
      Create a page with a template "one". Once the page is created/saved => create a new child page with the template of "two" If the parent is saved anytime after, do nothing to the child page (limit the parent page to one child page) The parent page is really just being used to output content, whereas the child page is being used to pull out the some fields from the parent to be used elsewhere. I might have made this too complicated in my head.
    • By Peter Knight
      Is there a way to make JPGs progressive by default via the API?
      I've added the following to my site/config.php file but user-uploaded images are often displayed as non progressive.
      $config->imageSizerOptions = array( 'upscaling' => true, // upscale if necessary to reach target size? 'cropping' => true, // crop if necessary to reach target size? 'autoRotation' => true, // automatically correct orientation? 'interlace' => true, // use interlaced JPEGs by default? (recommended) 'sharpening' => 'soft', // sharpening: none | soft | medium | strong 'quality' => 95, // quality: 1-100 where higher is better but bigger 'hidpiQuality' => 60, // Same as above quality setting, but specific to hidpi images 'defaultGamma' => 0.5, // defaultGamma: 0.5 to 4.0 or -1 to disable gamma correction (default=2.0) ); Thanks
    • By gregory
      Hi guys.
      I would like to show only the first image as a preview of a slideshow. The code below shows the first image but is repeated. Do you know a solution? Thank you
      //Call image foreach($story->galleria as $img) { echo "<img src='{$story->galleria->first()->url}' alt='{$story->galleria->first()->description}'>"; }