public function run() { $faker = Faker::create(); foreach (range(1, 10) as $index) { ImageManager::create([]); } }
/** * Process the upload of new logo */ public function processUploadLogo() { $error = ''; if (isset($_FILES['fileToUpload']['tmp_name']) && $_FILES['fileToUpload']['tmp_name']) { $file = $_FILES['fileToUpload']; $error = ImageManager::validateUpload($file, 300000); if (!strlen($error)) { $tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS'); if (!$tmp_name || !move_uploaded_file($file['tmp_name'], $tmp_name)) { return false; } list($width, $height, $type) = getimagesize($tmp_name); $newheight = $height > 500 ? 500 : $height; $percent = $newheight / $height; $newwidth = $width * $percent; $newheight = $height * $percent; if (!is_writable(_PS_ROOT_DIR_ . '/img/')) { $error = $this->l('Image folder %s is not writable', _PS_ROOT_DIR_ . '/img/'); } if (!$error) { list($src_width, $src_height, $type) = getimagesize($tmp_name); $src_image = ImageManager::create($type, $tmp_name); $dest_image = imagecreatetruecolor($src_width, $src_height); $white = imagecolorallocate($dest_image, 255, 255, 255); imagefilledrectangle($dest_image, 0, 0, $src_width, $src_height, $white); imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0, $src_width, $src_height, $src_width, $src_height); if (!imagejpeg($dest_image, _PS_ROOT_DIR_ . '/img/logo.jpg', 95)) { $error = $this->l('An error occurred during logo copy.'); } else { imagedestroy($dest_image); @chmod($filename, 0664); } } } else { $error = $this->l('An error occurred during logo upload.'); } } $this->ajaxJsonAnswer($error ? false : true, $error); }
/** * @deprecated 1.5.0 */ function createSrcImage($type, $filename) { Tools::displayAsDeprecated(); return ImageManager::create($type, $filename); }
/** * Cut image * * @param array $src_file Origin filename * @param string $dst_file Destination filename * @param integer $dst_width Desired width * @param integer $dst_height Desired height * @param string $file_type * @param int $dst_x * @param int $dst_y * * @return bool Operation result */ public static function cut($src_file, $dst_file, $dst_width = null, $dst_height = null, $file_type = 'jpg', $dst_x = 0, $dst_y = 0) { if (!file_exists($src_file)) { return false; } // Source information $src_info = getimagesize($src_file); $src = array('width' => $src_info[0], 'height' => $src_info[1], 'ressource' => ImageManager::create($src_info[2], $src_file)); // Destination information $dest = array(); $dest['x'] = $dst_x; $dest['y'] = $dst_y; $dest['width'] = !is_null($dst_width) ? $dst_width : $src['width']; $dest['height'] = !is_null($dst_height) ? $dst_height : $src['height']; $dest['ressource'] = ImageManager::createWhiteImage($dest['width'], $dest['height']); $white = imagecolorallocate($dest['ressource'], 255, 255, 255); imagecopyresampled($dest['ressource'], $src['ressource'], 0, 0, $dest['x'], $dest['y'], $dest['width'], $dest['height'], $dest['width'], $dest['height']); imagecolortransparent($dest['ressource'], $white); $return = ImageManager::write($file_type, $dest['ressource'], $dst_file); return $return; }
private function watermarkByImage($imagepath, $watermarkpath, $outputpath) { $Xoffset = $Yoffset = $xpos = $ypos = 0; list($tmp_width, $tmp_height, $type) = getimagesize($imagepath); $image = ImageManager::create($type, $imagepath); if (!$image) { return false; } if (!($imagew = imagecreatefromgif($watermarkpath))) { die($this->l('The watermark image is not a real GIF, please CONVERT the image.')); } list($watermarkWidth, $watermarkHeight) = getimagesize($watermarkpath); list($imageWidth, $imageHeight) = getimagesize($imagepath); if ($this->xAlign == 'middle') { $xpos = $imageWidth / 2 - $watermarkWidth / 2 + $Xoffset; } if ($this->xAlign == 'left') { $xpos = 0 + $Xoffset; } if ($this->xAlign == 'right') { $xpos = $imageWidth - $watermarkWidth - $Xoffset; } if ($this->yAlign == 'middle') { $ypos = $imageHeight / 2 - $watermarkHeight / 2 + $Yoffset; } if ($this->yAlign == 'top') { $ypos = 0 + $Yoffset; } if ($this->yAlign == 'bottom') { $ypos = $imageHeight - $watermarkHeight - $Yoffset; } if (!imagecopymerge($image, $imagew, $xpos, $ypos, 0, 0, $watermarkWidth, $watermarkHeight, $this->transparency)) { return false; } switch ($type) { case IMAGETYPE_PNG: $type = 'png'; break; case IMAGETYPE_GIF: $type = 'gif'; break; case IMAGETYPE_JPEG: $type = 'jpg'; break; } imagealphablending($image, false); imagesavealpha($image, true); return ImageManager::write($type, $image, $outputpath); }
/** * 缩放图片 * @param $src_file * @param $dst_file * @param null $dst_width * @param null $dst_height * @param string $file_type * @param bool $force_type * @return bool */ public static function resize($src_file, $dst_file, $dst_width = null, $dst_height = null, $file_type = 'jpg', $force_type = false) { if (PHP_VERSION_ID < 50300) { clearstatcache(); } else { clearstatcache(true, $src_file); } if (!file_exists($src_file) || !filesize($src_file)) { return false; } list($src_width, $src_height, $type) = getimagesize($src_file); if ($type == IMAGETYPE_PNG && !$force_type) { $file_type = 'png'; } //两不限 if ((int) $dst_width <= 0 && (int) $dst_height <= 0) { $dst_width = $src_width; $dst_height = $src_height; } elseif ((int) $dst_width > 0 && (int) $dst_height <= 0) { $dst_height = round($src_height * ($dst_width / $src_width)); } elseif ((int) $dst_width <= 0 && (int) $dst_height > 0) { $dst_width = round($src_width * ($dst_height / $src_height)); } $src_image = ImageManager::create($type, $src_file); $src_x = 0; $src_y = 0; $width_diff = $dst_width / $src_width; $height_diff = $dst_height / $src_height; if ($width_diff > 1 && $height_diff > 1) { $dst_width = $src_width; $dst_height = $src_height; } else { if ($width_diff < $height_diff) { $tmp_src_width = $dst_width * $src_height / $dst_height; $src_x = ($src_width - $tmp_src_width) / 2; $src_width = $tmp_src_width; } else { $tmp_src_height = $dst_height * $src_width / $dst_width; $src_y = ($src_height - $tmp_src_height) / 2; $src_height = $tmp_src_height; } } if (!ImageManager::checkImageMemoryLimit($src_file)) { return false; } $dest_image = imagecreatetruecolor($dst_width, $dst_height); // If image is a PNG and the output is PNG, fill with transparency. Else fill with white background. if ($file_type == 'png' && $type == IMAGETYPE_PNG) { imagealphablending($dest_image, false); imagesavealpha($dest_image, true); $transparent = imagecolorallocatealpha($dest_image, 255, 255, 255, 127); imagefilledrectangle($dest_image, 0, 0, $dst_width, $dst_height, $transparent); } else { $white = imagecolorallocate($dest_image, 255, 255, 255); imagefilledrectangle($dest_image, 0, 0, $dst_width, $dst_height, $white); } imagecopyresampled($dest_image, $src_image, 0, 0, $src_x, $src_y, $dst_width, $dst_height, $src_width, $src_height); return ImageManager::write($file_type, $dest_image, $dst_file); }
/** * Resize, cut and optimize image * * Zoom & Croop when the destination file name * contains the '_timthumb' phrase * - Modified by www.bazingadesigns.com/en * (TimThumb ZoomCrop port) * * @param string $src_file Image object from $_FILE * @param string $dst_file Destination filename * @param integer $dst_width Desired width (optional) * @param integer $dst_height Desired height (optional) * @param string $file_type * @return boolean Operation result */ public static function crop($src_file, $dst_file, $dst_width = null, $dst_height = null, $file_type = 'jpg', $force_type = false) { if (!file_exists($src_file)) { return false; } list($src_width, $src_height, $type) = getimagesize($src_file); // If PS_IMAGE_QUALITY is activated, the generated image will be a PNG with .jpg as a file extension. // This allow for higher quality and for transparency. JPG source files will also benefit from a higher quality // because JPG reencoding by GD, even with max quality setting, degrades the image. if (Configuration::get('PS_IMAGE_QUALITY') == 'png_all' || Configuration::get('PS_IMAGE_QUALITY') == 'png' && $type == IMAGETYPE_PNG && !$force_type) { $file_type = 'png'; } if (!$src_width) { return false; } if (!$dst_width) { $dst_width = 0; } if (!$dst_height) { $dst_height = 0; } $src_image = ImageManager::create($type, $src_file); $width_diff = $dst_width / $src_width; $height_diff = $dst_height / $src_height; if ($dst_width > 0 && $dst_height < 1) { $dst_height = floor($src_height * ($dst_width / $src_width)); } else { if ($dst_height > 0 && $dst_width < 1) { $dst_width = floor($src_width * ($dst_height / $src_height)); } } $src_x = $src_y = 0; $src_w = $src_width; $src_h = $src_height; $cmp_x = $src_width / $dst_width; $cmp_y = $src_height / $dst_height; if ($cmp_x > $cmp_y) { $src_w = round($src_width / $cmp_x * $cmp_y); $src_x = round(($src_width - $src_width / $cmp_x * $cmp_y) / 2); } else { if ($cmp_y > $cmp_x) { $src_h = round($src_height / $cmp_y * $cmp_x); $src_y = round(($src_height - $src_height / $cmp_y * $cmp_x) / 2); } } $dest_image = imagecreatetruecolor($dst_width, $dst_height); // If image is a PNG and the output is PNG, fill with transparency. Else fill with white background. if ($file_type == 'png' && $type == IMAGETYPE_PNG) { imagealphablending($dest_image, false); imagesavealpha($dest_image, true); $transparent = imagecolorallocatealpha($dest_image, 255, 255, 255, 127); imagefilledrectangle($dest_image, 0, 0, $dst_width, $dst_height, $transparent); } else { $white = imagecolorallocate($dest_image, 255, 255, 255); imagefilledrectangle($dest_image, 0, 0, $dst_width, $dst_height, $white); } imagecopyresampled($dest_image, $src_image, 0, 0, $src_x, $src_y, $dst_width, $dst_height, $src_w, $src_h); return ImageManager::write($file_type, $dest_image, $dst_file); }