/** * #2063 - get_resized_image_from_existing_file() fails asked for image larger than selection and not scaling an image up * Test get_image_resize_parameters(). */ public function testElggResizeImage() { $orig_width = 100; $orig_height = 150; // test against selection > max $options = array('maxwidth' => 50, 'maxheight' => 50, 'square' => true, 'upscale' => false, 'x1' => 25, 'y1' => 75, 'x2' => 100, 'y2' => 150); // should get back the same x/y offset == x1, y1 and an image of 50x50 $params = get_image_resize_parameters($orig_width, $orig_height, $options); $this->assertEqual($params['newwidth'], $options['maxwidth']); $this->assertEqual($params['newheight'], $options['maxheight']); $this->assertEqual($params['xoffset'], $options['x1']); $this->assertEqual($params['yoffset'], $options['y1']); // test against selection < max $options = array('maxwidth' => 50, 'maxheight' => 50, 'square' => true, 'upscale' => false, 'x1' => 75, 'y1' => 125, 'x2' => 100, 'y2' => 150); // should get back the same x/y offset == x1, y1 and an image of 25x25 because no upscale $params = get_image_resize_parameters($orig_width, $orig_height, $options); $this->assertEqual($params['newwidth'], 25); $this->assertEqual($params['newheight'], 25); $this->assertEqual($params['xoffset'], $options['x1']); $this->assertEqual($params['yoffset'], $options['y1']); }
/** * Gets the jpeg contents of the resized version of an already uploaded image * (Returns false if the file was not an image) * * @param string $input_name The name of the file on the disk * @param int $maxwidth The desired width of the resized image * @param int $maxheight The desired height of the resized image * @param bool $square If set to true, takes the smallest of maxwidth and * maxheight and use it to set the dimensions on the new image. * If no crop parameters are set, the largest square that fits * in the image centered will be used for the resize. If square, * the crop must be a square region. * @param int $x1 x coordinate for top, left corner * @param int $y1 y coordinate for top, left corner * @param int $x2 x coordinate for bottom, right corner * @param int $y2 y coordinate for bottom, right corner * @param bool $upscale Resize images smaller than $maxwidth x $maxheight? * * @return false|mixed The contents of the resized image, or false on failure */ function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0, $upscale = FALSE) { // Get the size information from the image $imgsizearray = getimagesize($input_name); if ($imgsizearray == FALSE) { return FALSE; } $width = $imgsizearray[0]; $height = $imgsizearray[1]; $accepted_formats = array('image/jpeg' => 'jpeg', 'image/pjpeg' => 'jpeg', 'image/png' => 'png', 'image/x-png' => 'png', 'image/gif' => 'gif'); // make sure the function is available $load_function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']]; if (!is_callable($load_function)) { return FALSE; } // get the parameters for resizing the image $options = array('maxwidth' => $maxwidth, 'maxheight' => $maxheight, 'square' => $square, 'upscale' => $upscale, 'x1' => $x1, 'y1' => $y1, 'x2' => $x2, 'y2' => $y2); $params = get_image_resize_parameters($width, $height, $options); if ($params == FALSE) { return FALSE; } // load original image $original_image = $load_function($input_name); if (!$original_image) { return FALSE; } // allocate the new image $new_image = imagecreatetruecolor($params['newwidth'], $params['newheight']); if (!$new_image) { return FALSE; } // color transparencies white (default is black) imagefilledrectangle($new_image, 0, 0, $params['newwidth'], $params['newheight'], imagecolorallocate($new_image, 255, 255, 255)); $rtn_code = imagecopyresampled($new_image, $original_image, 0, 0, $params['xoffset'], $params['yoffset'], $params['newwidth'], $params['newheight'], $params['selectionwidth'], $params['selectionheight']); if (!$rtn_code) { return FALSE; } // grab a compressed jpeg version of the image ob_start(); imagejpeg($new_image, NULL, 90); $jpeg = ob_get_clean(); imagedestroy($new_image); imagedestroy($original_image); return $jpeg; }