Jump to content
*Most Powerful Pony!*

How can I use $image->size, from small to change the size of the image canvas?

Recommended Posts

I have an image 500x300 

I need to apply the resizing to 150x150 without cutting edge. 

Thus in any case, I need to have the image canvas size 150x150 

When you use $ image->size (150, 150), we obtain the desired result (cropping). 

When using $ image->size (150, 150, array ('cropping' => false)) our image circumcised according to the greatest height, canvas change proportionally. / / 150x90. 

Need an image 150x90 position in the center of the canvas 150x150. 

You can do this using the API?

post-2003-0-65413500-1392706786_thumb.pn

Share this post


Link to post
Share on other sites

$ratioX = 150 / $image->width;

$ratioY = 150 / $image->height;

// the least expensive, is taken for size caculation

$width = min($ratioX, $ratioY) * $image->width;

$height = min($ratioX, $ratioY) * $image->height;

$image->size($width,$height);

Share this post


Link to post
Share on other sites

post-2003-0-65413500-1392706786_thumb.pn

You did draw: resize the image till one of the edges reach 150px, can be width or can be height or both if the original is square.

if you don't want this, do you want to mesh up with the aspect ratio ? 

  • Like 1

Share this post


Link to post
Share on other sites

I'm not sure if I understand right, but may be you can use PageImageManipulator for that?

$img = $images->first();
$url = $img->pimLoad('myprefix')->canvas(150, 150, array(255,255,255,1), 'c', 0)->pimSave()->url;

Share this post


Link to post
Share on other sites

First I don't understand why you would send "unnecessary" pixels over the internet. I would say, don't send them and make the space you need with the HTML container element.

Second, why do want to spoil server CPU for this.

Third, if you want to make it your self difficult. here is the documentation.

  • Like 1

Share this post


Link to post
Share on other sites

  GD spoils color in JPG

Oh, really? Please, can you show me a example? (original | GD | and maybe one with imageMagick)

  • Like 1

Share this post


Link to post
Share on other sites

I did thought you were talking about canvas color that can be created with GD, not about incorrectness or not assigning of color profiles to images while resizing. 

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 Spyros
      Hello
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      PS 
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By rushy
      Hi all
      Continuing my first project where I am creating and manipulating stuff from the frontend. Till now I've always added things like images from the backend, but in this project I need to add and move images from a frontend control. This is a photo album where images are stored in albums, each album being a page containing a Pageimages array in the usual way. So what I want to know is how do I move an image from one album (page) to another in the frontend? I just need some guidance on how to approach this.  I suppose I need to do a copy and delete - but how do I copy an image or images from one page to another? What function should I use to create a new image on an existing page? I include a code snippet from the server side of my delete image request and it works fine. I'd like to implement something similar for a move and upload new image request. 
      Many thanks for any help. Paul 
      <?php namespace ProcessWire; // sanitize inputs as 1-line text $action = $sanitizer->text($input->post('action')); $instr = $sanitizer->text($input->post('input')); $sel = $input->post('selected'); // Expect JSON for image selected image list $selected = json_decode($sel); $nosel = count($selected); $response = array(); // for building JSON response switch($action) { // delete selected images case 'delete': $out = "<p>Deleted $nosel image(s)</p>"; foreach($selected as $item){ $album = $pages->get($item->album); $album->of(false); $out .= "<p>Image {$item->file} from album {$album->title}</p>"; $album->images->delete($item->file); $album->of(true); } $out .= saveUpdatedAlbums($pages, $selected); // add the response message for the delete $response['message'] = $out; break; ...... // save any album that had an image deleted function saveUpdatedAlbums($pages, $selected) { $cur = ''; $out = ''; foreach($selected as $item){ $album = $pages->get($item->album); if($album->id != $cur) { $album->of(false); $album->save('images'); $album->of(true); $cur = $album->id; $out .= "<p>Updated album {$pages->get($cur)->title}</p>"; } } return $out; }  
       
       
    • By Robin S
      Inspired by a recent question.
      Image Crop Ratios
      Allows preset aspect ratios to be defined per image field for the ProcessWire image crop tool.
      The module adds a select dropdown to the crop tool. Choose an aspect ratio and the crop area will be fixed to that ratio.
      Screencast

      Installation
      Install the Image Crop Ratios module.
      Configuration
      Default aspect ratios for all image fields can be defined in the module config. Aspect ratios for specific image fields can be defined on the Input tab of the field settings. You can override the ratio settings in template context if needed. Insert a hyphen as the first item in the ratio settings unless you want to force a ratio to be applied to the crop tool. The hyphen represents a blank option that allows a free crop area to be drawn. Usage
      Click the "Crop" link on the details view of an image thumbnail. Click the "Crop" icon at the top of the editor window. Choose an option from the "Ratio" select dropdown.  
      https://github.com/Toutouwai/ImageCropRatios
      https://modules.processwire.com/modules/image-crop-ratios/
    • By Guy Incognito
      This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! 🙂 
      foreach ($propertyImages as $img) { $fileName = trim($img[0]); if ( !empty($fileName) ) { $imgPath = '../property_data/'.$fileName; if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) { $p->property_images->add($imgPath); $p->save(); $newImg = $p->property_images->last(); $newImg->description = $img[1]; $p->save(); } } }  
×
×
  • Create New...