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 louisstephens
      I am wondering, how do you pass a variable into wire('page')->get() inside a function? I have been looking through the forums, but unfortunately I have not found the answer yet.
      My current set up is:
      function generateNewPages($parentPageName) { $p = new Page(); $p->template = "parent"; $p->parent = wire('pages')->get('/home/'); $p->name = $parentPageName; $p->title = $parentPageName; $p->of(false); $p->save(); $p2 = new Page(); $p2->template = "child"; $p2->parent = wire('pages')->get('$parentPageName'); $p2->name = "child"; $p2->title = "Child Page"; $p2->of(false); $p2->save(); } When I try to run it by passing in a title like generateNewPages('Demo');, "Demo" is created, but when it gets to the child page I get:
      Unknown Selector operator: '$' -- was your selector value properly escaped?
      Is there a way to pass the $parentPageName to "wire('pages')->get('$parentPageName')" ?
    • By thomasaull
      I recently started to build Vue SPAs with ProcessWire as the backend, connected with a REST API. Thanks to code and the help of @LostKobrakai (How to use FastRoute with ProcessWire) and @clsource (REST-Helper) I got it up and running pretty quickly and now have put all of it in a site profile for others to use. It includes the REST API with routing for different endpoints, JWT Auth and a simple Vue SPA which shows the process of logging in a user (nevertheless, you don't have to use the Vue part, the API will work on it's own).
      Check it out here: https://github.com/thomasaull/RestApiProfile
      I'm pretty sure, it's not the perfect or most sophisticsted solution, but it gets the job done for me…
      Feedback or Improvements are very welcome
      Update: This site profile is a module now: https://github.com/thomasaull/RestApi
    • By Pixrael
      I have a technical question that maybe you can guide me to a solution/idea.
      I know an online tool that publishes product information and updates prices/inventories regularly from marketplaces such as Amazon, Ebay, etc. to a Wordpress website ... can get the orders information, auto-order it and send back the tracking numbers. All this is done through the Woocommerce API. They don't have an API or CSV option to access this features..
      Is it possible to create fake REST endpoints (a clone of WooCommerce) on my site to accept requests from that external website and process this data my way inside PW? The requests to a REST endpoint are POST like in regular forms submits?
      Sorry I don't have to much technical background about this
      https://woocommerce.github.io/woocommerce-rest-api-docs/?php#create-a-product https://woocommerce.github.io/woocommerce-rest-api-docs/?php#update-a-product https://woocommerce.github.io/woocommerce-rest-api-docs/?php#retrieve-an-order
    • By Xonox
      Hi,
       
      I'm trying to upload images from a folder into a page. I need to replace the images instead of adding. The image field already has the replace existing images turned on, but it doesn't seem to be enough.
       
      My code:
      foreach($files as $file) { if(substr($file, 0, 1) != '.' && $file != '.' && $file != '..') { // Get SKU $file_sku = substr($file, 0, 9); // Check for book $book = $pages->get('sku=' . $file_sku); if(!$book->id) { // Book doesn't exist } else { // Upload image into book !!! THIS CODE ADDS FILE INSTEAD OF REPLACING. HOW CAN I REPLACE? $book->book_images->add($upload_directory . '/' . $file); // Delete file unlink($upload_directory . '/' . $file); } // Save book if($book->id) $book->save(); } }  
      What am I missing?
    • By louisstephens
      I have a repeater with a text field where I would like to use hanna code (to make it easier on the user) so the user can define the action of a image that is in the repeater. For example:
      1. The image should have an external link:
      - the user can just type in the url
      2. The image, when clicked, should be printable (using print.js)
      - the user can just type [[print_image]]
      That works just fine, but I am running into an "issue". The code I need to use to print is:
      onclick="printJS({printable: 'path_to_image', type: 'image'});" I can put that into the hanna code as plain text, but I have no idea how to get the image from the repeater item and pass to "path_to_image". Is this even possible?