function resizeThumbnailImage($thumb, $image, $width, $height, $start_width, $start_height, $scale) { $newImageWidth = ceil($width * $scale); $newImageHeight = ceil($height * $scale); $newImage = imagecreatetruecolor($newImageWidth, $newImageHeight); $ext = strtolower(substr(basename($image), strrpos(basename($image), '.') + 1)); $source = ''; if ($ext == 'png') { $source = imagecreatefrompng($image); } elseif ($ext == 'jpg' || $ext == 'jpeg') { $source = imagecreatefromjpeg($image); } elseif ($ext == 'gif') { $source = imagecreatefromgif($image); } imagecopyresampled($newImage, $source, 0, 0, $start_width, $start_height, $newImageWidth, $newImageHeight, $width, $height); $ext = strtolower(substr(basename($thumb), strrpos(basename($thumb), '.') + 1)); if ($ext == 'png') { $result = imagepng($newImage, $thumb, 0); } elseif ($ext == 'jpg' || $ext == 'jpeg') { $result = imagejpeg($newImage, $thumb, 90); } elseif ($ext == 'gif') { $result = imagegif($newImage, $thumb); } if (!$result) { return false; } chmod($thumb, 0664); return $thumb; }
function newimg() { //改变后的图象的比例 $resize_ratio = $this->resize_width / $this->resize_height; //实际图象的比例 $ratio = $this->width / $this->height; if ($this->cut == "1") { if ($ratio >= $resize_ratio) { $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->height * $resize_ratio, $this->height); ImageJpeg($newimg, $this->dstimg); } if ($ratio < $resize_ratio) { $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, $this->width / $resize_ratio); ImageJpeg($newimg, $this->dstimg); } } else { if ($ratio >= $resize_ratio) { $newimg = imagecreatetruecolor($this->resize_width, $this->resize_width / $ratio); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_width / $ratio, $this->width, $this->height); ImageJpeg($newimg, $this->dstimg); } if ($ratio < $resize_ratio) { $newimg = imagecreatetruecolor($this->resize_height * $ratio, $this->resize_height); imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_height * $ratio, $this->resize_height, $this->width, $this->height); ImageJpeg($newimg, $this->dstimg); } } }
function resize($photo_src, $width, $name) { $parametr = getimagesize($photo_src); list($width_orig, $height_orig) = getimagesize($photo_src); $ratio_orig = $width_orig / $height_orig; $new_width = $width; $new_height = $width / $ratio_orig; $newpic = imagecreatetruecolor($new_width, $new_height); $col2 = imagecolorallocate($newpic, 255, 255, 255); imagefilledrectangle($newpic, 0, 0, $new_width, $new_width, $col2); switch ($parametr[2]) { case 1: $image = imagecreatefromgif($photo_src); break; case 2: $image = imagecreatefromjpeg($photo_src); break; case 3: $image = imagecreatefrompng($photo_src); break; } imagecopyresampled($newpic, $image, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig); imagejpeg($newpic, $name, 100); return true; }
/** * Automatically resizes an image and returns formatted IMG tag * * @param string $path Path to the image file, relative to the webroot/img/ directory. * @param integer $width Image of returned image * @param integer $height Height of returned image * @param boolean $aspect Maintain aspect ratio (default: true) * @param array $htmlAttributes Array of HTML attributes. * @param boolean $return Wheter this method should return a value or output it. This overrides AUTO_OUTPUT. * @return mixed Either string or echos the value, depends on AUTO_OUTPUT and $return. * @access public */ public function resize($path, $width, $height, $aspect = true, $htmlAttributes = array(), $return = false) { $types = array(1 => "gif", "jpeg", "png", "swf", "psd", "wbmp"); // used to determine image type if (empty($htmlAttributes['alt'])) { $htmlAttributes['alt'] = 'thumb'; } // Ponemos alt default $uploadsDir = 'uploads'; $fullpath = ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS . $uploadsDir . DS; $url = ROOT . DS . APP_DIR . DS . WEBROOT_DIR . DS . $path; if (!($size = getimagesize($url))) { return; } // image doesn't exist if ($aspect) { // adjust to aspect. if ($size[1] / $height > $size[0] / $width) { // $size[0]:width, [1]:height, [2]:type $width = ceil($size[0] / $size[1] * $height); } else { $height = ceil($width / ($size[0] / $size[1])); } } $relfile = $this->webroot . $uploadsDir . '/' . $this->cacheDir . '/' . $width . 'x' . $height . '_' . basename($path); // relative file $cachefile = $fullpath . $this->cacheDir . DS . $width . 'x' . $height . '_' . basename($path); // location on server if (file_exists($cachefile)) { $csize = getimagesize($cachefile); $cached = $csize[0] == $width && $csize[1] == $height; // image is cached if (@filemtime($cachefile) < @filemtime($url)) { // check if up to date $cached = false; } } else { $cached = false; } if (!$cached) { $resize = $size[0] > $width || $size[1] > $height || ($size[0] < $width || $size[1] < $height); } else { $resize = false; } if ($resize) { $image = call_user_func('imagecreatefrom' . $types[$size[2]], $url); if (function_exists("imagecreatetruecolor") && ($temp = imagecreatetruecolor($width, $height))) { imagecopyresampled($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); } else { $temp = imagecreate($width, $height); imagecopyresized($temp, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); } call_user_func("image" . $types[$size[2]], $temp, $cachefile); imagedestroy($image); imagedestroy($temp); } else { //copy($url, $cachefile); } return $this->output(sprintf($this->Html->tags['image'], $relfile, $this->Html->_parseAttributes($htmlAttributes, null, '', ' ')), $return); }
namespace common; class upload { //从tmp中移动upload public static function zoom(&$file, &$maxWidth = 0, &$maxHeight = 0) { list($width, $height, $im, $func, $ext) = self::_init($file); if (!$im) { \yk\log::runlog('file upload error: im not found', 'upload'); return false; } if ($maxWidth > 0) { $p = max($width / $maxWidth, $height / $maxHeight); $dstwidth = intval($width / $p); $dstheight = intval($height / $p); } else { $dstwidth = $width; $dstheight = $height; } $maxWidth = $dstwidth; $maxHeight = $dstheight; $dstim = imagecreatetruecolor($dstwidth, $dstheight); imagealphablending($dstim, false); //关闭混杂模式,不可缺少, PHP文档中说明: (在非混色模式下,画笔颜色连同其 alpha 通道信息一起被拷贝,替换掉目标像素。混色模式在画调色板图像时不可用。) 而且是imagesavealpha方法起作用的前置步骤. imagesavealpha($dstim, true); //保存 PNG 图像时保存完整的 alpha 通道信息 $transparent = imagecolorallocatealpha($dstim, 255, 255, 255, 127); //取得一个透明的颜色, 透明度在 0-127 间 imagefill($dstim, 0, 0, $transparent); imagecopyresampled($dstim, $im, 0, 0, 0, 0, $dstwidth, $dstheight, $width, $height); $file = uniqid() . $ext;
public function cropnsave($id) { $basepath = app_path() . '/storage/uploads/'; $jpeg_quality = 90; $src = $basepath . 'resize_' . $id; if (ImageModel::getImgTypeByExtension($id) == ImageModel::IMGTYPE_PNG) { $img_r = imagecreatefrompng($src); } else { $img_r = imagecreatefromjpeg($src); } $dst_r = ImageCreateTrueColor(Input::get('w'), Input::get('h')); imagecopyresampled($dst_r, $img_r, 0, 0, Input::get('x'), Input::get('y'), Input::get('w'), Input::get('h'), Input::get('w'), Input::get('h')); $filename = $basepath . 'crop_' . $id; imagejpeg($dst_r, $filename, $jpeg_quality); $image = ImageModel::createImageModel('crop_' . $id); try { $session = Session::get('user'); $userService = new SoapClient(Config::get('wsdl.user')); $currentUser = $userService->getUserById(array("userId" => $session['data']->id)); $currentUser = $currentUser->user; $currentUser->avatar = $image; $result = $userService->updateUser(array("user" => $currentUser)); // Cleanup File::delete($src); File::delete($filename); File::delete($basepath . $id); return Response::json($result->complete); } catch (Exception $ex) { error_log($ex); } }
public function filter($value) { if (!file_exists($value)) { throw new Zend_Filter_Exception('Image does not exist: ' . $value); } $image = imagecreatefromstring(file_get_contents($value)); if (false === $image) { throw new Zend_Filter_Exception('Can\'t load image: ' . $value); } // find ratio to scale down to // TODO: pass 600 as parameter in the future $origWidth = imagesx($image); $origHeight = imagesy($image); $ratio = max($origWidth, $origHeight) / 600; if ($ratio > 1) { // img too big! create a scaled down image $newWidth = round($origWidth / $ratio); $newHeight = round($origHeight / $ratio); $resized = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($resized, $image, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight); // determine type and store to disk $explodeResult = explode(".", $value); $type = strtolower($explodeResult[count($explodeResult) - 1]); $writeFunc = 'image' . $type; if ($type == 'jpeg' || $type == 'jpg') { imagejpeg($resized, $value, 100); } else { $writeFunc($resized, $value); } } return $value; }
public static function image($item, $path, $filename) { $image_tempname1 = $_FILES[$item]['name']; $ImageName = $path . $image_tempname1; $newfilename = $path . $filename; if (move_uploaded_file($_FILES[$item]['tmp_name'], $ImageName)) { list($width, $height, $type, $attr) = getimagesize($ImageName); if ($type == 2) { rename($ImageName, $newfilename); } else { if ($type == 1) { $image_old = imagecreatefromgif($ImageName); } elseif ($type == 3) { $image_old = imagecreatefrompng($ImageName); } $image_jpg = imagecreatetruecolor($width, $height); imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0, $width, $height, $width, $height); imagejpeg($image_jpg, $newfilename); imagedestroy($image_old); imagedestroy($image_jpg); } return 1; } else { return 0; } }
function create_pic($upfile, $new_path, $width) { $quality = 100; $image_path = $upfile; $image_info = getimagesize($image_path); $exname = ''; //1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF switch ($image_info[2]) { case 1: @($image = imagecreatefromgif($image_path)); $exname = 'gif'; break; case 2: @($image = imagecreatefromjpeg($image_path)); $exname = 'jpg'; break; case 3: @($image = imagecreatefrompng($image_path)); $exname = 'png'; break; case 6: @($image = imagecreatefromwbmp($image_path)); $exname = 'wbmp'; break; } $T_width = $image_info[0]; $T_height = $image_info[1]; if (!empty($image)) { $image_x = imagesx($image); $image_y = imagesy($image); } else { return FALSE; } @chmod($new_path, 0777); if ($image_x > $width) { $x = $width; $y = intval($x * $image_y / $image_x); } else { @copy($image_path, $new_path . '.' . $exname); return $exname; } $newimage = imagecreatetruecolor($x, $y); imagecopyresampled($newimage, $image, 0, 0, 0, 0, $x, $y, $image_x, $image_y); switch ($image_info[2]) { case 1: imagegif($newimage, $new_path . '.gif', $quality); break; case 2: imagejpeg($newimage, $new_path . '.jpg', $quality); break; case 3: imagepng($newimage, $new_path . '.png', $quality); break; case 6: imagewbmp($newimage, $new_path . '.wbmp', $quality); break; } imagedestroy($newimage); return $exname; }
/** * {@inheritdoc} */ public function apply(&$image) { $inputWidth = imagesx($image); $inputHeight = imagesy($image); $width = $inputWidth; $height = $inputHeight; // bigger if ($height < $this->outputHeight) { $width = $this->outputHeight / $height * $width; $height = $this->outputHeight; } if ($width < $this->outputWidth) { $height = $this->outputWidth / $width * $height; $width = $this->outputWidth; } // taller if ($height > $this->outputHeight) { $width = $this->outputHeight / $height * $width; $height = $this->outputHeight; } // wider if ($width > $this->outputWidth) { $height = $this->outputWidth / $width * $height; $width = $this->outputWidth; } $output = imagecreatetruecolor($width, $height); imagecopyresampled($output, $image, 0, 0, 0, 0, $width, $height, $inputWidth, $inputHeight); $image = $output; }
function resize_image($file, $w, $h, $crop = FALSE) { list($width, $height) = getimagesize($file); $r = $width / $height; if ($crop) { if ($width > $height) { $width = ceil($width - $width * abs($r - $w / $h)); } else { $height = ceil($height - $height * abs($r - $w / $h)); } $newwidth = $w; $newheight = $h; } else { if ($w / $h > $r) { $newwidth = $h * $r; $newheight = $h; } else { $newheight = $w / $r; $newwidth = $w; } } $src = imagecreatefromjpeg($file); $dst = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); return $dst; }
function resizer($filename, $copypath, $MaxWidth, $MaxHeight) { //cesta k souboru, ktery chcete zmensit cesta, kam zmenseny soubor ulozit maximalni sirka zmenseneho obrazku //maximalni vyska zmenseneho obrazku //zjistime puvodni velikost obrazku list($OrigWidth, $OrigHeight) = getimagesize($filename); //hodnota 0 v parametrech MaxWidth resp. MaxHeight znamena, ze sirka resp. vyska vysledku muze byt libovolna if ($MaxWidth == 0) { $MaxWidth = $OrigWidth; } if ($MaxHeight == 0) { $MaxHeight = $OrigHeight; } //nyni vypocitam pomer zmenseni $pw = $OrigWidth / $MaxWidth; $ph = $OrigHeight / $MaxHeight; if ($pw > $ph) { $p = $pw; } else { $p = $ph; } if ($p < 1) { $p = 1; } //v p ted mame pomer pro zmenseni vypocitame vysku a sirku zmenseneho obrazku $NewWidth = (int) $OrigWidth / $p; $NewHeight = (int) $OrigHeight / $p; //vytvorime novy obrazek pozadovane vysky a sirky $image_p = imagecreatetruecolor($NewWidth, $NewHeight); //otevreme puvodni obrazek se souboru $image = imagecreatefromjpeg($filename); //a okopirujeme zmenseny puvodni obrazek do noveho imagecopyresampled($image_p, $image, 0, 0, 0, 0, $NewWidth, $NewHeight, $OrigWidth, $OrigHeight); //a ulozime imagejpeg($image_p, $copypath, 100); }
function scale($maxW, $maxH, $fitType = FALSE) { if (!is_resource($this->prcImg['resoc'])) { $this->prcImg = $this->srcImg; } $w = $this->prcImg['w']; $h = $this->prcImg['h']; $xRatio = $maxW / $w; $yRatio = $maxH / $h; if ($w <= $maxW && $h <= $maxH) { $newW = $maxW; $newH = $maxH; } else { if ($xRatio * $h < $maxH) { $newH = ceil($xRatio * $h); $newW = $maxW; } else { $newW = ceil($yRatio * $w); $newH = $maxH; } } if ($fitType == 'BOTH') { $newW = $maxW; $newH = $maxH; } $imageHandle = imagecreatetruecolor($newW, $newH); imagecopyresampled($imageHandle, $this->prcImg['resoc'], 0, 0, 0, 0, $newW, $newH, $this->prcImg['w'], $this->prcImg['h']); $this->prcImg['resoc'] = $imageHandle; }
/** * Wrapper function for 'imagecopyresampled' * * @param Image $image * @param integer $dst_x * @param integer $dst_y * @param integer $src_x * @param integer $src_y * @param integer $dst_w * @param integer $dst_h * @param integer $src_w * @param integer $src_h * @return boolean */ protected function modify($image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { foreach ($image as $frame) { // create new image $modified = imagecreatetruecolor($dst_w, $dst_h); // get current image $resource = $frame->getCore(); // preserve transparency $transIndex = imagecolortransparent($resource); if ($transIndex != -1) { $rgba = imagecolorsforindex($modified, $transIndex); $transColor = imagecolorallocatealpha($modified, $rgba['red'], $rgba['green'], $rgba['blue'], 127); imagefill($modified, 0, 0, $transColor); imagecolortransparent($modified, $transColor); } else { imagealphablending($modified, false); imagesavealpha($modified, true); } // copy content from resource imagecopyresampled($modified, $resource, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); // free memory of old core imagedestroy($resource); // set new content as recource $frame->setCore($modified); } return true; }
function upload($tmp, $name, $nome, $larguraP, $pasta) { $ext = strtolower(end(explode('.', $name))); if ($ext == 'jpg') { $img = imagecreatefromjpeg($tmp); } elseif ($ext == 'gif') { $img = imagecreatefromgif($tmp); } else { $img = imagecreatefrompng($tmp); } $x = imagesx($img); $y = imagesy($img); $largura = $x > $larguraP ? $larguraP : $x; $altura = $largura * $y / $x; if ($altura > $larguraP) { $altura = $larguraP; $largura = $altura * $x / $y; } $nova = imagecreatetruecolor($largura, $altura); imagecopyresampled($nova, $img, 0, 0, 0, 0, $largura, $altura, $x, $y); imagejpeg($nova, "{$pasta}/{$nome}"); imagedestroy($img); imagedestroy($nova); return $nome; }
function foundation_process_image_file($file_name, $setting_name) { if ($setting_name->domain == FOUNDATION_SETTING_DOMAIN && $setting_name->name == 'logo_image') { // Need to make sure this isn't too big if (function_exists('getimagesize') && function_exists('imagecreatefrompng') && function_exists('imagecopyresampled') && function_exists('imagepng')) { $size = getimagesize($file_name); if ($size) { $width = $size[0]; $height = $size[1]; if ($size['mime'] == 'image/png') { if ($width > FOUNDATION_MAX_LOGO_SIZE) { $new_width = FOUNDATION_MAX_LOGO_SIZE; $new_height = $height * $new_width / $width; $src_image = imagecreatefrompng($file_name); $saved_image = imagecreatetruecolor($new_width, $new_height); // Preserve Transparency imagecolortransparent($saved_image, imagecolorallocate($saved_image, 0, 0, 0)); imagecopyresampled($saved_image, $src_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // Get rid of the old file unlink($file_name); // New image, compression level 5 (make it a bit smaller) imagepng($saved_image, $file_name, 5); } } } } } }
function createthumb($originalImage, $newName, $new_w, $new_h) { $src_img = imagecreatefromjpeg($originalImage); $newName .= ".jpg"; # Maintain proportions $old_x = imageSX($src_img); $old_y = imageSY($src_img); if ($old_x > $old_y) { $thumb_w = $new_w; $thumb_h = $old_y * ($new_h / $old_x); } if ($old_x < $old_y) { $thumb_w = $old_x * ($new_w / $old_y); $thumb_h = $new_h; } if ($old_x == $old_y) { $thumb_w = $new_w; $thumb_h = $new_h; } # Create destination-image-resource $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h); # Copy source-image-resource to destination-image-resource imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); # Create the final image from the destination-image-resource imagejpeg($dst_img, $newName); # Delete our image-resources imagedestroy($dst_img); imagedestroy($src_img); # Show results return $newName; }
public function generate() { $im_width = $this->_owner->getImageWidth(); $im_height = $this->_owner->getImageHeight(); //Resample before detection? $ratio = 0; $diff_width = 320 - $im_width; $diff_height = 240 - $im_height; if ($diff_width > $diff_height) { $ratio = $im_width / 320; } else { $ratio = $im_height / 240; } if ($ratio != 0) { $this->reduced_canvas = imagecreatetruecolor($im_width / $ratio, $im_height / $ratio); imagecopyresampled($this->reduced_canvas, $this->_owner->image, 0, 0, 0, 0, $im_width / $ratio, $im_height / $ratio, $im_width, $im_height); $stats = $this->getImageStats($this->reduced_canvas); $this->face = $this->detectGreedyBigToSmall($stats['ii'], $stats['ii2'], $stats['width'], $stats['height']); if ($this->face['w'] > 0) { $this->face['x'] *= $ratio; $this->face['y'] *= $ratio; $this->face['w'] *= $ratio; } } else { $stats = $this->getImageStats($this->_owner->image); $this->face = $this->detectGreedyBigToSmall($stats['ii'], $stats['ii2'], $stats['width'], $stats['height']); } return $this->face['w'] > 0; }
public function resize($originalImageResource, $targetImageWidth, $targetImageHeight) { $origWidth = imagesx($originalImageResource); $origHeight = imagesy($originalImageResource); if ($origWidth < $targetImageWidth) { $targetImageWidth = $origWidth; } if ($origHeight < $targetImageHeight) { $targetImageHeight = $origHeight; } $origRatio = $origHeight / $origWidth; $newRatio = $targetImageHeight / $targetImageWidth; $dst_x = 0; $dst_y = 0; $src_x = 0; $src_y = 0; if ($origRatio < $newRatio) { $dst_w = $targetImageWidth; $dst_h = $targetImageWidth * $origRatio; $src_w = $origWidth; $src_h = $origHeight; } else { $dst_w = $targetImageHeight / $origRatio; $dst_h = $targetImageHeight; $src_w = $origWidth; $src_h = $origHeight; } $targetImageResource = imagecreatetruecolor($dst_w, $dst_h); imagecopyresampled($targetImageResource, $originalImageResource, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); return $targetImageResource; }
public function resize($width = 0, $height = 0, $scaled = true) { if (!$this->info['width'] || !$this->info['height']) { return; } $xpos = 0; $ypos = 0; $scale = min($width / $this->info['width'], $height / $this->info['height']); if ($scale == 1) { return; } if ($scaled) { $new_width = round($this->info['width'] * $scale); $new_height = round($this->info['height'] * $scale); $xpos = round(($width - $new_width) / 2); $ypos = round(($height - $new_height) / 2); } else { $new_width = $width; $new_height = $height; } $image_old = $this->image; $this->image = imagecreatetruecolor($width, $height); $this->image = $this->transparency($this, imagecreatetruecolor($width, $height)); imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']); imagedestroy($image_old); $this->info['width'] = $width; $this->info['height'] = $height; }
function UploadImage($bukti_name) { //direktori gambar $vdir_upload = "bukti/"; $vfile_upload = $vdir_upload . $bukti_name; //Simpan gambar dalam ukuran sebenarnya move_uploaded_file($_FILES["bukti"]["tmp_name"], $vfile_upload); //identitas file asli $im_src = imagecreatefromjpeg($vfile_upload); $src_width = imageSX($im_src); $src_height = imageSY($im_src); //Simpan dalam versi small 110 pixel //Set ukuran gambar hasil perubahan $dst_width = 50; $dst_height = $dst_width / $src_width * $src_height; //proses perubahan ukuran $im = imagecreatetruecolor($dst_width, $dst_height); imagecopyresampled($im, $im_src, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); //Simpan gambar imagejpeg($im, $vdir_upload . "small_" . $bukti_name); //Simpan dalam versi medium 360 pixel //Set ukuran gambar hasil perubahan $dst_width2 = 270; $dst_height2 = $dst_width2 / $src_width * $src_height; //proses perubahan ukuran $im2 = imagecreatetruecolor($dst_width2, $dst_height2); imagecopyresampled($im2, $im_src, 0, 0, 0, 0, $dst_width2, $dst_height2, $src_width, $src_height); //Simpan gambar imagejpeg($im2, $vdir_upload . "medium_" . $bukti_name); //Hapus gambar di memori komputer imagedestroy($im_src); imagedestroy($im); imagedestroy($im2); }
function max_height($a_max) { $max = intval($a_max) * ($this->m_width / $this->m_height); $dst = imagecreatetruecolor($max, intval($a_max)); imagecopyresampled($dst, $this->m_handle, 0, 0, 0, 0, $max, intval($a_max), $this->m_width, $this->m_height); $this->set($dst); }
function mkthumb($img_src, $img_width = "100", $img_height = "100", $folder_scr = "include/files", $des_src = "include/files") { // Größe und Typ ermitteln list($src_width, $src_height, $src_typ) = getimagesize($folder_scr . "/" . $img_src); if (!$src_typ) { return false; } // calculate new size if ($src_width >= $src_height) { $new_image_height = $src_height / $src_width * $img_width; $new_image_width = $img_width; if ($new_image_height > $img_height) { $new_image_width = $new_image_width / $new_image_height * $img_height; $new_image_height = $img_height; } } else { $new_image_width = $src_width / $src_height * $img_height; $new_image_height = $img_height; if ($new_image_width > $img_width) { $new_image_height = $new_image_height / $new_image_width * $img_width; $new_image_width = $img_width; } } // for the case that the thumbnail would be bigger then the original picture if ($new_image_height > $src_height) { $new_image_width = $new_image_width * $src_height / $new_image_height; $new_image_height = $src_height; } if ($new_image_width > $src_width) { $new_image_height = $new_image_height * $new_image_width / $src_width; $new_image_width = $src_width; } if ($src_typ == 1) { $image = imagecreatefromgif($folder_scr . "/" . $img_src); $new_image = imagecreate($new_image_width, $new_image_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_image_width, $new_image_height, $src_width, $src_height); imagegif($new_image, $des_src . "/" . $img_src . "_thumb", 100); imagedestroy($image); imagedestroy($new_image); return true; } elseif ($src_typ == 2) { $image = imagecreatefromjpeg($folder_scr . "/" . $img_src); $new_image = imagecreatetruecolor($new_image_width, $new_image_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_image_width, $new_image_height, $src_width, $src_height); imagejpeg($new_image, $des_src . "/" . $img_src . "_thumb", 100); imagedestroy($image); imagedestroy($new_image); return true; } elseif ($src_typ == 3) { $image = imagecreatefrompng($folder_scr . "/" . $img_src); $new_image = imagecreatetruecolor($new_image_width, $new_image_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_image_width, $new_image_height, $src_width, $src_height); imagepng($new_image, $des_src . "/" . $img_src . "_thumb"); imagedestroy($image); imagedestroy($new_image); return true; } else { return false; } }
function thumb($filename, $destination = null, $dst_w = null, $dst_h = null, $isReservedSource = false, $scale = 0.5) { list($src_w, $src_h, $imagetype) = getimagesize($filename); if (is_null($dst_w) || is_null($dst_h)) { $dst_w = ceil($src_w * $scale); $dst_h = ceil($src_h * $scale); } $mime = image_type_to_mime_type($imagetype); $createFun = str_replace("/", "createfrom", $mime); $outFun = str_replace("/", null, $mime); $src_image = $createFun($filename); $dst_image = imagecreatetruecolor($dst_w, $dst_h); imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h); //image_50/sdfsdkfjkelwkerjle.jpg if ($destination && !file_exists(dirname($destination))) { mkdir(dirname($destination), 0777, true); } $dstFilename = $destination == null ? getUniName() . "." . getExt($filename) : $destination; $outFun($dst_image, $dstFilename); imagedestroy($src_image); imagedestroy($dst_image); if (!$isReservedSource) { unlink($filename); } return $dstFilename; }
function fastimagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3) { // Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled. // Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled". // Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting. // Author: Tim Eckel - Date: 09/07/07 - Version: 1.1 - Project: FreeRingers.net - Freely distributable - These comments must remain. // // Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example // 1.5. Must be greater than zero. // Between 0 and 1 = Fast, but mosaic results, closer to 0 increases the mosaic effect. // 1 = Up to 350 times faster. Poor results, looks very similar to imagecopyresized. // 2 = Up to 95 times faster. Images appear a little sharp, some prefer this over a quality of 3. // 3 = Up to 60 times faster. Will give high quality smooth results very close to imagecopyresampled, just faster. // 4 = Up to 25 times faster. Almost identical to imagecopyresampled for most images. // 5 = No speedup. Just uses imagecopyresampled, no advantage over imagecopyresampled. if (empty($src_image) || empty($dst_image) || $quality <= 0) { return false; } if ($quality < 5 && ($dst_w * $quality < $src_w || $dst_h * $quality < $src_h)) { $temp = imagecreatetruecolor($dst_w * $quality + 1, $dst_h * $quality + 1); imagecopyresized($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, $src_w, $src_h); imagecopyresampled($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, $dst_h * $quality); imagedestroy($temp); } else { imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } return true; }
function hacknrollify($picfilename) { $logofilename = "logo.png"; $logoPicPath = "logopics/" . $logofilename; $originalPicPath = "originalpics/" . $picfilename; $editedfilename = "hnr_" . $picfilename; $editedPicPath = "editedpics/" . $editedfilename; // read the original image from file $profilepic = imagecreatefromjpeg($originalPicPath); $profilepicWidth = imagesx($profilepic); $profilepicHeight = imagesy($profilepic); // create the black image overlay $blackoverlay = imagecreate($profilepicWidth, $profilepicHeight); imagecolorallocate($blackoverlay, 0, 0, 0); // then merge the black and profilepic imagecopymerge($profilepic, $blackoverlay, 0, 0, 0, 0, $profilepicWidth, $profilepicHeight, 50); imagedestroy($blackoverlay); // merge the resized logo $logo = resizeImage($logoPicPath, $profilepicWidth - 80, 999999); imageAlphaBlending($logo, false); imageSaveAlpha($logo, true); $logoWidth = imagesx($logo); $logoHeight = imagesy($logo); $verticalOffset = $profilepicHeight / 2 - $logoHeight / 2; $horizontalOffset = 40; imagecopyresampled($profilepic, $logo, $horizontalOffset, $verticalOffset, 0, 0, $logoWidth, $logoHeight, $logoWidth, $logoHeight); $mergeSuccess = imagejpeg($profilepic, $editedPicPath); if (!$mergeSuccess) { echo "Image merge failed!"; } imagedestroy($profilepic); imagedestroy($logo); return $editedPicPath; }
public function store_codeOp() { if ($_GET['text']) { $text = $_GET['text']; $size = '6'; $level = 'H'; $logo = BASE_RESOURCE_PATH . DS . '/logo_2.png'; $padding = '2'; $path = BASE_RESOURCE_PATH . DS . 'phpqrcode'; $QR = $path . 'qrcode.png'; require_once BASE_RESOURCE_PATH . DS . 'phpqrcode' . DS . 'phpqrcode.php'; QRcode::png($text, $QR, $level, $size, $padding); if ($logo !== false) { $QR = imagecreatefromstring(file_get_contents($QR)); $logo = imagecreatefromstring(file_get_contents($logo)); $QR_width = imagesx($QR); $QR_height = imagesy($QR); $logo_width = imagesx($logo); $logo_height = imagesy($logo); $logo_qr_width = $QR_width / 5; $scale = $logo_width / $logo_qr_width; $logo_qr_height = $logo_height / $scale; $from_width = ($QR_width - $logo_qr_width) / 2; imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height); } header("Content-Type:image/jpg"); imagepng($QR); } else { output_error('参数错误00023'); } }
function output() { $arr = $this->ret; $bg = DATA_DIR . '/cache/vcodebg.png'; $image = imagecreatefrompng($bg); list($w, $baseH) = getimagesize($bg); header('Content-type: image/png'); $x = 1; foreach ($arr as $i => $filename) { list($w, $h) = getimagesize($filename); $source = imagecreatefrompng($filename); $t_id = imagecolortransparent($source); $rotate = imagerotate($source, rand(-20, 20), $t_id); $w2 = $w * $baseH / $h; imagecopyresized($image, $rotate, $x, 0, 0, 0, $w2, $baseH, $w, $h); imagedestroy($source); imagedestroy($rotate); $x += $w2; } $x += 1; $dst = imagecreatetruecolor($x, $baseH); imagecopyresampled($dst, $image, 0, 0, 0, 0, $x, $baseH, $x, $baseH); imagepng($dst); imagedestroy($image); imagedestroy($dst); exit; }
function createthumb($originalImage, $new_w, $new_h) { $src_img = imagecreatefromjpeg("uploads/" . $originalImage); # Add the _t to our image name list($imageName, $extension) = explode(".", $originalImage); $newName = $imageName . "_t." . $extension; # Maintain proportions $old_x = imageSX($src_img); $old_y = imageSY($src_img); if ($old_x > $old_y) { $thumb_w = $new_w; $thumb_h = $old_y * ($new_h / $old_x); } if ($old_x < $old_y) { $thumb_w = $old_x * ($new_w / $old_y); $thumb_h = $new_h; } if ($old_x == $old_y) { $thumb_w = $new_w; $thumb_h = $new_h; } # Create destination-image-resource $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h); # Copy source-image-resource to destination-image-resource imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); # Create the final image from the destination-image-resource imagejpeg($dst_img, "uploads/" . $newName); # Delete our image-resources imagedestroy($dst_img); imagedestroy($src_img); # Show results return $newName; }
function resizer($image) { $name = md5(sha1(date('d-m-y H:i:s') . $image['tmp_name'])); $type = $image['type']; switch ($type) { case "image/jpeg": $img = imagecreatefromjpeg($image['tmp_name']); break; case "image/png": $img = imagecreatefrompng($image['tmp_name']); break; } $x = imagesx($img); $y = imagesy($img); $height = 1200 * $y / $x; $new = imagecreatetruecolor(1200, $height); imagecopyresampled($new, $img, 0, 0, 0, 0, 1200, $height, $x, $y); switch ($type) { case "image/jpeg": $local = "../assets/img/Portfolioimg/{$name}" . ".jpg"; imagejpeg($new, $local); break; case "image/png": $local = "../assets/img/Portfolioimg/{$name}" . ".png"; imagejpeg($new, $local); break; } return $local; }