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 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}'>"; }  
    • By louisstephens
      From my last post, I was given a good idea on how to count the repeater items, and it worked wonderfully. I got my code working well and the columns (based on the count) all work well as well. Now, I have a head scratcher on my hands. 
      <?php $buttonsIncluded = $page->special_custom_buttons->find('special_custom_buttons_include=1'); $buttonsIncludedCount = count($buttonsIncluded); $buttonsIncludedCountAdditional = $buttonsIncludedCount +1; echo $buttonsIncludedCount; ?> <div class="row"> <?php foreach($buttonsIncluded as $button): ?> <?php if($button->custom_buttons_include): ?> <?php if($buttonsIncludedCountAdditional == 2): ?> <div class="col-6"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php elseif($buttonsIncludedCountAdditional == 3): ?> <div class="col-4"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php elseif($buttonsIncludedCountAdditional == 4): ?> <div class="col-3"> <a href=""><?php echo $button->custom_buttons_text; ?></a> </div> <?php endif; ?> <?php endif; ?> <?php endforeach; ?> </div> All of this is included in a larger foreach statement that is pulling in other data (like body copy etc etc) from a Page Table field. As you can see in my code above, I am adding "1" to the count, so I can have space in the grid layout for a new button.
      So, right now: it looks something like: 
      [repeater button] [repeater button] [repeater button] [space for new button] What I really need to do is to pull in the button from the Page Table and add it into the new space so it looks like:
      [repeater button] [repeater button] [repeater button] [button from Page Table] Is this even possible todo, or is there a better way to go about this? 
       
      *Edit*
      So, I really just overlooked something quite easy here. Since the grid is based on 12 columns, I could just take 12 and divide by $buttonsIncludedCountAdditional which would give me the remaining col width to use outside the foreach loop. I was trying to make this too complicated.
    • By louisstephens
      Is it possible to use count() to return a number of repeater items don't have a checkbox checked? In my current set up, I have a repeater on the page "dev_repeater" with a checkbox called "dev_repeater_exclude". I need to get a count of the current items that do not have it checked so I can pass it to my css grid to alter the column width.
    • By awebcreature
      Hi all,
      I have a small project which need to get records from Immowelt.de through API.  These records must be on specific user who has entered them. I find the documentation of this API but i don't find something about such selection of user related records. All parameters are for all records in immowelt.de database without user related selection. 
      https://www.immowelt.de/ImmoweltAG/InternetProdukte/api-immowelt.pdf
      Anyone with experience with this Immowelt.de API?  
    • By Arunesh Dutta
      Hello all
      I am newbie.Wanted to know does processwire will allow to display external website content and other sources to my website using API powered by processwire