Změna velikosti obrázku a následné uložení - PHP
Stále se učím s OOP, tak prosím nepište věci typu že je to prasečina apod.. Já to vím ;) ..
Ale k věci.
V kódu je vidět že si dokážu zjistit rozlišení obrázku a následně si přepočítat. Ovšem už tu hledám nějakou dobu jak vlastně ten obrázek upravit a následně uložit. Našel jsem funkce jako imagecopyresized, apod.. ovšem ani jedno se mi nepodařilo.
<?php
setlocale(LC_ALL, 'czech'); // záleží na použitém systému
class photoUploadManager{
protected $registry;
private $file_addres = array();
private $file_name = array();
private $photo_dir = 'photos/';
private $max_width;
private $max_height;
function __construct($registry){
$this->registry = $registry;
}
/**
* ošetření vstupu
* převedení UTF 8 (zbavení diakritiky)
* odstranění určitých znaků
*
* @param mixed $string
*/
private function treatment($string){
$string = str_replace(' ','_',$string);
$string = str_replace('"','',$string);
$string = str_replace('\'','',$string);
$string = str_replace('#','',$string);
$string = str_replace('?','',$string);
$string = str_replace('!','',$string);
$string = iconv("utf-8", "ASCII//TRANSLIT", $string);
return $string;
}
/**
* otestování zda existuje defaultní složka pro fotky
*/
public function exists_photo_folder(){
if(is_dir($this->photo_dir.date('m_Y'))){
$this->set_folder($this->photo_dir.date('m_Y'));
return true;
}
else{
if($this->exists_folder()){
if(mkdir($this->photo_dir.date('m_Y'))){
$this->set_folder($this->photo_dir.date('m_Y'));
return true;
}
}
else{
if(mkdir($this->photo_dir))
{
if(mkdir(date('m_Y'))){
$this->set_folder($this->photo_dir.date('m_Y'));
return true;
}
}
}
}
return false;
}
/**
* otestování zda existuje výchozí složka pro fotky
*/
private function exists_folder(){
if(is_dir($this->photo_dir)){
return true;
}
else{
if(mkdir($this->photo_dir)){
return true;
}
}
return false;
}
public function save_photo($tmp_name,$name,$type,$size){
$image_info = getimagesize($tmp_name.'/'.$name);
$image_info_big = $this->resize($image_info,$this->max_width,$this->max_height);
$image_info_small = $this->resize($image_info,150,150);
$data = file_get_contents($tmp_name.'/'.$name);
$file_name = date('d_H_i_s').'.jpg';
$small_photo['name'] = 's'.$file_name;
$small_photo['link'] = $this->photo_dir. $small_photo['name'];
$small_photo['size'] = file_put_contents($small_photo['link'],$data);
$small_photo['width'] = $image_info_small[0];
$small_photo['height'] = $image_info_small[1];
$data = file_get_contents($tmp_name.'/'.$name);
$big_photo['name'] = 'b'.$file_name;
$big_photo['link'] = $this->photo_dir.$big_photo['name'];
$big_photo['size'] = file_put_contents($big_photo['link'],$data);
$big_photo['width'] = $image_info_big[0];
$big_photo['height'] = $image_info_big[1];
return array('original'=>$big_photo,'smaller'=>$small_photo);
}
/**
* nastavení výchozí složky pro fotky
* @param mixed $folder
*/
public function set_folder($folder){
$folder = $this->treatment($folder);
if(substr($folder,-1)== "/"){
$this->photo_dir = $folder;
}
else{
$this->photo_dir = $folder."/";
}
}
private function resize($image_info,$new_height,$new_width){
if($image_info[1] > $image_info[0]){
$height = $new_height;
$counter = $image_info[1] / $new_height;
$width = floor($image_info[0] /$counter);
}
else{
$width = $new_width;
$counter = $image_info[0] / $new_width;
$height = floor($image_info[1] / $counter);
}
$image_info[0]=$width;
$image_info[1]=$height;
$image_info[3]="width=\"$width\" height=\"$height\"";
return $image_info;
}
public function set_resolution($width = 1024,$height = 768){
$this->max_height = floor($height);
$this->max_width = floor($width);
}
}
Třeba
zkusil jsem zakomponovat do svého
Warning: imagecopyresampled() expects parameter 2 to be resource, string given in .....\Fotogalery\UploadManager.php on line 95
Podívejte se, co je dáno jako druhý argument funkci imagecopyresampled().
zda chápu a umím trochu anglicky, tak Source image link resource. je adresa na originál... což je to co tam vkládám
Je tam důležité to slovo "resource". Takže Vy předáváte string s cestou, ale funkce chce proměnnou typu resource s původním obrázkem.
Nechce se mi studovat váš kód, ale druhý argument musí být něco jako -
Děkuji.. s fotkama jsem ještě nedělal.. už mi to funguje jak má.