Jump to content

Upload image to product with webservice api.


ijipk

Recommended Posts

Hi 

 

I'm trying to upload a image to a product, but I cant. 

this i my code for downloading an image and then try to upload it, but the uploading part fails. it says "Image added" but I don't find the picture under the product I specified. 

 

the original code says this. but it a outdated command, so I tried to replace it. 

curl_setopt($ch, CURLOPT_POSTFIELDS, array('image'=>"@".$img_path.";type=image/jpeg"));

 

with

$args['file'] = new CurlFile($img_path, 'image/gif', "image");

curl_setopt($ch, CURLOPT_POSTFIELDS, $args);

 

here is the code 



function image($remoteImageURL,$id){


// save the image to local folder
$dir_path_to_save = 'images/'; 

class GetImage {


var $source;
var $save_to;
var $set_extension;
var $quality;

function download($method = 'curl') // default method: cURL
{
$info = @GetImageSize($this->source);
$mime = $info['mime'];

if(!$mime) exit('Could not obtain mime-type information. Make sure that the remote file is actually a valid image.');

// What sort of image?
$type = substr(strrchr($mime, '/'), 1);

switch ($type) 
{
case 'jpeg':
    $image_create_func = 'ImageCreateFromJPEG';
    $image_save_func = 'ImageJPEG';
	$new_image_ext = 'jpg';
	
	// Best Quality: 100
	$quality = isSet($this->quality) ? $this->quality : 100; 
    break;

case 'png':
    $image_create_func = 'ImageCreateFromPNG';
    $image_save_func = 'ImagePNG';
	$new_image_ext = 'png';
	
	// Compression Level: from 0  (no compression) to 9
	$quality = isSet($this->quality) ? $this->quality : 0;
    break;

case 'bmp':
    $image_create_func = 'ImageCreateFromBMP';
    $image_save_func = 'ImageBMP';
	$new_image_ext = 'bmp';
    break;

case 'gif':
    $image_create_func = 'ImageCreateFromGIF';
    $image_save_func = 'ImageGIF';
	$new_image_ext = 'gif';
    break;

case 'vnd.wap.wbmp':
    $image_create_func = 'ImageCreateFromWBMP';
    $image_save_func = 'ImageWBMP';
	$new_image_ext = 'bmp';
    break;

case 'xbm':
    $image_create_func = 'ImageCreateFromXBM';
    $image_save_func = 'ImageXBM';
	$new_image_ext = 'xbm';
    break;

default: 
	$image_create_func = 'ImageCreateFromJPEG';
    $image_save_func = 'ImageJPEG';
	$new_image_ext = 'jpg';
}

if(isSet($this->set_extension))
{
$ext = strrchr($this->source, ".");
$strlen = strlen($ext);
$new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext;
}
else
{
$new_name = basename($this->source);
}

$save_to = $this->save_to.$new_name;

    if($method == 'curl')
	{
    $save_image = $this->LoadImageCURL($save_to);
	}
	elseif($method == 'gd')
	{
	$img = $image_create_func($this->source);

	    if(isSet($quality))
	    {
		   $save_image = $image_save_func($img, $save_to, $quality);
		}
		else
		{
		   $save_image = $image_save_func($img, $save_to);
		}
	}
	
	return $save_image;
}

function LoadImageCURL($save_to)
{
$ch = curl_init($this->source);
$fp = fopen($save_to, "wb");

// set URL and other appropriate options
$options = array(CURLOPT_FILE => $fp,
                 CURLOPT_HEADER => 0,
                 CURLOPT_FOLLOWLOCATION => 1,
	             CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough)

curl_setopt_array($ch, $options);

$save = curl_exec($ch);
curl_close($ch);
fclose($fp);

return $save;
}
}


// initialize the class
$image = new GetImage;



$image->source = $remoteImageURL;

$image->save_to = $dir_path_to_save; // with trailing slash at the end

$get = $image->download('curl'); // using GD

if($get)
{
echo "The image has been saved.";
}

$image_name = basename($remoteImageURL);


// change the local path where image has been downloaded "presta-api" is my local folder from where i run API script
$img_path = 'http://dirtytime.dk/minmappe/images/8527.jpg';


//image will be associated with product id 4
$url = PS_SHOP_PATH. '/api/images/products/'.$id;
 
$ch = curl_init();
//$cfile = array('image'=>"@".$img_path.";type=image/jpeg");

$args['file'] = new CurlFile($img_path, 'image/gif', "image");

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_PUT, true); To edit a picture

curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


if(curl_exec($ch) === false)
{ 
echo "<br><br>Error : ".curl_error($ch)."<br>"; }
else { echo '<br><br> Image added'; }
curl_close($ch);


}

Edited by ijipk (see edit history)
Link to comment
Share on other sites

I solved it myself. 

 

here are the working code for PHP 5.5


function image($remoteImageURL,$id){


// save the image to local folder
$dir_path_to_save = 'images/'; 

class GetImage {


var $source;
var $save_to;
var $set_extension;
var $quality;

function download($method = 'curl') // default method: cURL
{
$info = @GetImageSize($this->source);
$mime = $info['mime'];

if(!$mime) exit('Could not obtain mime-type information. Make sure that the remote file is actually a valid image.');

// What sort of image?
$type = substr(strrchr($mime, '/'), 1);

switch ($type) 
{
case 'jpeg':
    $image_create_func = 'ImageCreateFromJPEG';
    $image_save_func = 'ImageJPEG';
	$new_image_ext = 'jpg';
	
	// Best Quality: 100
	$quality = isSet($this->quality) ? $this->quality : 100; 
    break;

case 'png':
    $image_create_func = 'ImageCreateFromPNG';
    $image_save_func = 'ImagePNG';
	$new_image_ext = 'png';
	
	// Compression Level: from 0  (no compression) to 9
	$quality = isSet($this->quality) ? $this->quality : 0;
    break;

case 'bmp':
    $image_create_func = 'ImageCreateFromBMP';
    $image_save_func = 'ImageBMP';
	$new_image_ext = 'bmp';
    break;

case 'gif':
    $image_create_func = 'ImageCreateFromGIF';
    $image_save_func = 'ImageGIF';
	$new_image_ext = 'gif';
    break;

case 'vnd.wap.wbmp':
    $image_create_func = 'ImageCreateFromWBMP';
    $image_save_func = 'ImageWBMP';
	$new_image_ext = 'bmp';
    break;

case 'xbm':
    $image_create_func = 'ImageCreateFromXBM';
    $image_save_func = 'ImageXBM';
	$new_image_ext = 'xbm';
    break;

default: 
	$image_create_func = 'ImageCreateFromJPEG';
    $image_save_func = 'ImageJPEG';
	$new_image_ext = 'jpg';
}

if(isSet($this->set_extension))
{
$ext = strrchr($this->source, ".");
$strlen = strlen($ext);
$new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext;
}
else
{
$new_name = basename($this->source);
}

$save_to = $this->save_to.$new_name;

    if($method == 'curl')
	{
    $save_image = $this->LoadImageCURL($save_to);
	}
	elseif($method == 'gd')
	{
	$img = $image_create_func($this->source);

	    if(isSet($quality))
	    {
		   $save_image = $image_save_func($img, $save_to, $quality);
		}
		else
		{
		   $save_image = $image_save_func($img, $save_to);
		}
	}
	
	return $save_image;
}

function LoadImageCURL($save_to)
{
$ch = curl_init($this->source);
$fp = fopen($save_to, "wb");

// set URL and other appropriate options
$options = array(CURLOPT_FILE => $fp,
                 CURLOPT_HEADER => 0,
                 CURLOPT_FOLLOWLOCATION => 1,
	             CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough)

curl_setopt_array($ch, $options);

$save = curl_exec($ch);
curl_close($ch);
fclose($fp);

return $save;
}
}


// initialize the class
$image = new GetImage;



$image->source = $remoteImageURL;

$image->save_to = $dir_path_to_save; // with trailing slash at the end

$get = $image->download('curl'); // using GD

if($get)
{
echo "The image has been saved.";
}

$image_name = basename($remoteImageURL);


// change the local path where image has been downloaded "presta-api" is my local folder from where i run API script
$img_path = '/home/dirtytim/public_html/minmappe/images/8527.jpg';


//image will be associated with product id 4
$url = PS_SHOP_PATH. '/api/images/products/'.$id;
 
$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_PUT, true); To edit a picture

curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image'=> new CurlFile($img_path)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


if(curl_exec($ch) === false)
{ 
echo "<br><br>Error : ".curl_error($ch)."<br>"; }
else { echo '<br><br> Image added'; }
curl_close($ch);

}
Link to comment
Share on other sites

  • 2 months later...

Hello, I'm trying to edit a product image but it doesn't work. My code is:

public function editImage($prod_id,$img,$img_id){
		$img_url= (Yii::getAlias('@app').'/web/img/'.$img);
				
		$url = PS_SHOP_PATH. 'api/images/products/'.$prod_id.'/'.$img_id;

		//$cfile = curl_file_create($img_url,'image/png', 'image');
		$cfile = new \CURLFile($img_url,'image/png', 'image');
		
		//$data = array("image" => $cfile);

		$ch = curl_init();
		$headers = array("Content-Type:multipart/form-data"); 
		//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($ch, CURLOPT_URL, $url);
		//curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_PUT, true);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER, 0);
		curl_setopt($ch, CURLOPT_HTTPAUTH, 'CURLAUTH_BASIC');
		curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $cfile);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		
		if(curl_exec($ch) === false)
		{ 
		echo "<br><br>Error : ".curl_error($ch)."<br>"; }
		else { echo '<br><br> Image added'; }
		curl_close($ch);

		//$result = curl_exec($ch);
		//curl_close($ch);
		//return($result);
	}

Do you have any suggestion?

Link to comment
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
×
×
  • Create New...