We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50264
    • 15 Posts
    Hi All,

    I'm struggling to get this working. Probably missing something stupidly simple - any help is appreciated:

    I'm storing an pThumb/Image+ json string within a MIGX field. It looks like this:

    { 
    	"sourceImg": { 
    		"src": "assets/image.png", 
    		"width": 1440, 
    		"height": 900, 
    		"source": 6 
    	}, 
    	"crop": {
    		"width": 800, 
    		"height": 600, 
    		"x": 560, 
    		"y": 134 
    	}, 
    	"targetWidth": 800, 
    	"targetHeight": 600, 
    	"altTag": "This is an alt tag."
    }
    


    All I'm trying to do is take this JSON string and run it through pThumb to generate a cropped image (cached).

    Here is how I'm currently trying to do it:

    $imageJson = json_decode('{the JSON string: above}');
    $options .= "w={$imageJson->targetWidth}&h={$imageJson->targetHeight}&sx={$imageJson->crop->x}&sy={$imageJson->crop->y}";
    
    $output .= $modx->getChunk('renderPrimaryContentImage',
    	array(
    		'input' => '/../../domain/web/public-assets/img/'.$imageJson->sourceImg->src,
    		'options' => $options,
    		'debug' => 'true',
    	)
    );
    


    And my chunk:

    [[!pthumb? &input=`[[+input]]` &options=`[[+options]]` &debug=`1`]]


    I've also tried using runSnippet and calling pthumb direct to no avail.

    This is working in the sense that the cached image is resizing and saving where it's supposed to be saved HOWEVER the image isn't cropping as 'cropped' in the Image+ crop popup which of course the $imageJson->crop->x and $imageJson->crop->y variables come from.

    So in short:

    How do I get pThumb to crop an image? (not just zoom crop, but to the actual co-ordinates I've defined in the image+ crop tool)?

    Any and all help is greatly appreciated.

    Matt









    This question has been answered by Jako. See the first response.

    • discuss.answer
      Use the ImagePlus snippet in the getImageList output chunk:

      [[ImagePlus? 
      &tvname=`whateverTV` 
      &docid=`[[+id]]`
      &options=`w=100` 
      ]]
      

      to get an url to the cropped and resized thumbnail.

      or
      [[ImagePlus? 
      &tvname=`whateverTV` 
      &docid=`[[+id]]`
      &options=`w=100` 
      &type=`tpl` 
      &tpl=`whateverImagePlusTemplate`
      ]]

      to get an parsed result of whateverImagePlusTemplate chunk.
        • 50264
        • 15 Posts
        Thank you! I didn't even think (know) about the ImagePlus snippet.

        I ended up using the snippet directly, below is a breakdown of the my particular problem/solution for future people searching:

        Problem:

        I have an Image+ (ImagePlus) and pThumb json string stored in a MIGX variable field and cannot correctly save a cached cropped version of the image.

        Solution:

        Use the ImagePlus snippet directly and pass through the following options:

        $modx->runSnippet('ImagePlus', array('value' => $yourJsonString, 'type' => 'tpl', 'tpl' => 'yourImageChunkTemplate.chunk.html'))


        And yourImageChunkTemplate.chunk.html could look like this:

        <img src="[[+url]]" />